2015-09-02 107 views
0

我创建了一个链接到本地​​数据库的应用程序。它工作正常,但唯一的问题是,在按下插入按钮后,数据将插入到数据库中,但只有在关闭并重新打开应用程序后才显示在GridView中。在按下插入值的按钮后,如何才能使数据正确显示?谢谢 !DataGridView在插入本地数据库后不显示数据

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()); 
       } 

      } 
     } 
     } 
+0

dataGridView1.ReBind(); –

+0

您应该选择数据并再次绑定datagridview。 – Arash

+0

它显示一个错误,因为System.DataGrindView不包含“重新绑定”的定义。为什么? – Ezekiel

回答

0

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

喜欢的东西:

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

DataGridView不包含dataBind的定义。为什么? – Ezekiel

+0

啊,对不起你使用System.Windows.Forms,请改为dataGridView1.SetDataBinding(SomeDataSet,“TableName”); –

+0

相同。没有定义SetDataBinding。 – Ezekiel

0

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

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

这不是测试,我可能误会了一些名字,因为我在这里没有安装VS做,但你的想法。不是最优雅的解决方案,但它为我工作。你也可以尝试dataGridView1.Refresh() - 它不适合我。

+0

不包含ParentControl错误的定义。 – Ezekiel

+0

是的,它只是父母,我告诉你我没有VS:P我编辑了代码 –

+0

没有工作,有些错误。 – Ezekiel