【问题标题】:Duplicate the old data when the datagridview refreshed刷新datagridview时复制旧数据
【发布时间】:2013-09-15 11:24:45
【问题描述】:

我已经可以将新数据添加到数据库并通过datagridview显示该数据库,并且每当我单击“添加”按钮(将新数据添加到数据库)时,我都会命令刷新datagridview。但它是旧数据与新数据的重复。

这是添加新数据并刷新 datagridview 后显示重复的屏幕截图:

在上图中,ID为“16”的重复数据,每当我输入新值并将其插入数据库时​​,它会将新(当前)值添加到数据库中,但旧值会重复.因此,我必须退出该程序并重新启动它。但是,有没有其他方法可以解决这个问题(向数据库添加新数据并刷新datagridview后旧数据不重复)?

这是我的代码:

private void ViewDatabase(object sender, EventArgs e)
        {
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                string query = "SELECT * FROM [Table]";

                conn.Open();

                using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
                {
                    adapter.Fill(ds, "Table");
                    dataGridView1.DataSource = ds.Tables[0];
                }

                conn.Close();
            }
        }


private void Add(object sender, EventArgs e)
        {
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                string query = "INSERT INTO [Table] ([ProductCode], [Quantity], [Description], [Price]) VALUES (@ProductCode, @Quantity, @Description, @Price)";

                conn.Open();

                using (OleDbCommand cmd = new OleDbCommand(query, conn))
                {
                    cmd.Parameters.Add("@ProductCode", System.Data.OleDb.OleDbType.Integer);
                    cmd.Parameters["@ProductCode"].Value = this.numericTextBox1.Text;

                    cmd.Parameters.Add("@Quantity", System.Data.OleDb.OleDbType.Integer);
                    cmd.Parameters["@Quantity"].Value = this.numericUpDown1.Value;

                    cmd.Parameters.Add("@Description", System.Data.OleDb.OleDbType.VarChar);
                    cmd.Parameters["@Description"].Value = this.textBox3.Text;

                    cmd.Parameters.Add("@Price", System.Data.OleDb.OleDbType.Integer);
                    cmd.Parameters["@Price"].Value = this.textBox4.Text;

                    cmd.ExecuteNonQuery();

                    if (choice.comboBox1.Text == "English")
                    {
                        System.Media.SoundPlayer sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
                        sound.Play();

                        DialogResult dialogResult = MessageBox.Show("Added Successfully!", "Success", MessageBoxButtons.OK);

                        if (dialogResult == DialogResult.OK)
                        {
                            ViewDatabase(sender, e);

                            ClearTextBoxes(sender, e);
                        }
                    }

【问题讨论】:

    标签: c# winforms ms-access datagridview


    【解决方案1】:

    插入这个

    dataGridView1.DataSource = null;
    

    这里

    adapter.Fill(ds, "Table");
    //here
    dataGridView1.DataSource = ds.Tables[0];
    

    编辑:

    移动

                        dataGridView1.DataSource = ds.Tables[0];
    

    在嵌套的 using 块之外。

    【讨论】:

    • hai ahsan sallem,我已经通过在adapter.Fill(ds, "Table") 下方添加dataGridView.DataSource = null; 尝试了您的代码,但它仍然重复旧数据。
    • 试试这个适配器。Fill(ds, "Table"); dataGridView1.DataSource = null; dataGridView1.Refresh();并移动这一行“dataGridView1.DataSource = ds.Tables[0];”在嵌套的 using 块之外。
    • 不,它仍然不能@Ahsan Saleem
    猜你喜欢
    • 2017-01-17
    • 2010-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    相关资源
    最近更新 更多