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

本文关键字:显示 数据 数据库 插入 DataGridView | 更新日期: 2023-09-27 18:10:55

我创建了一个链接到本地数据库的应用程序。它工作得很好,但唯一的问题是,在我按下插入按钮后,数据被插入到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());
                }
            }
        }
        }

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

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

类似:

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

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

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

它没有经过测试,我可能搞错了一些名字,因为我没有在这里安装一个VS,但你得到的想法。这不是最优雅的解决方案,但对我来说很有效。你也可以尝试dataGridView1.Refresh() -它不适合我。