DataGridView在插入本地数据库后不显示数据

时间:2015-09-02 12:05:13

标签: c# mysql datagridview

我创建了一个链接到本地​​数据库的应用。它运行良好,但唯一的问题是,在我按下插入按钮后,数据被插入到db中,但只有在我关闭并重新打开应用程序后才会在GridView中显示。按下插入值的按钮后,如何才能显示数据?谢谢!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.IO;

namespace Gradinita
{
    public partial class Grupa : Form
    {
        string nume = "";
        List<Label> labels = new List<Label>();
        public Grupa(string nume)
        {
            InitializeComponent();
            this.nume = nume;

        }

        private void Grupa_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'grupeDataSet8.copii' table. You can move, or remove it, as needed.
            this.copiiTableAdapter2.Fill(this.grupeDataSet8.copii);
            // TODO: This line of code loads data into the 'grupeDataSet7.copii' table. You can move, or remove it, as needed.
            this.copiiTableAdapter1.Fill(this.grupeDataSet7.copii);
            // TODO: This line of code loads data into the 'grupeDataSet3.copii' table. You can move, or remove it, as needed.
            this.copiiTableAdapter.Fill(this.grupeDataSet3.copii);
            var connString = (@"Data Source=" + System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)) + @"\Grupe.sdf");
            using (var conn = new SqlCeConnection(connString))
            {
                try
                {
                    conn.Open();
                    var query = "SELECT * FROM grupe WHERE Nume='" + nume + "'";
                    var command = new SqlCeCommand(query, conn);
                    var dataAdapter = new SqlCeDataAdapter(command);
                    var dataTable = new DataTable();
                    dataAdapter.Fill(dataTable);

                    label1.Text = dataTable.Rows[0][0].ToString();
                    label2.Text = dataTable.Rows[0][1].ToString();
                    label3.Text = dataTable.Rows[0][2].ToString();
                    label4.Text = dataTable.Rows[0][3].ToString();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
        }
    }

        private void button1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                label5.Text = ("1");
            }
            if (checkBox2.Checked)
            {
                label5.Text = ("0");
            }
            textBox1.Text = (Convert.ToInt32(textBox5.Text) - Convert.ToInt32(textBox6.Text)).ToString();
            var connString = (@"Data Source=" + Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + @"\Grupe.sdf");
            using (var conn = new SqlCeConnection(connString))
            {
                try
                {
                    conn.Open();
                    var query = "INSERT INTO copii(prezenta, Nume, Prenume, Program, Taxa, Achitat, Diferenta) VALUES('" + label5.Text + "', '" + textBox2.Text.Trim() + "', '" + textBox3.Text.Trim() + "', '" + textBox4.Text.Trim() + "', '" + textBox5.Text.Trim() + "', '"+ textBox6.Text.Trim()+"', '"+ textBox1.Text.Trim() +"');";
                    MessageBox.Show(query);
                    var command = new SqlCeCommand(query, conn);
                    command.ExecuteNonQuery();
                    dataGridView1.Refresh();  //not working obviously

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

            }
        }
        }

2 个答案:

答案 0 :(得分:0)

您需要重新查询数据并重新绑定数据表。

类似的东西:

dataGridView1.DataSource = SomeDataTableSource;
dataGridView1.DataBind();

答案 1 :(得分:0)

我设法通过重新添加网格到控件来绕过这个。首先,在变量中复制网格,然后从父控件中删除它,然后将变量添加到该控件的控件中。

var grid = dataGridView1.Parent.Controls["dataGridView1"];
var ctr = dataGridView1.Parent;
ctr.Controls.Remove(dataGridView1);
ctr.Controls.Add(grid);

它没有经过测试,我可能误会了一些名字因为我没有在这里安装VS,但你明白了。不是最优雅的解决方案,但它对我有用。你也可以试试dataGridView1.Refresh() - 它对我没用。