繁体   English   中英

插入数据库后刷新DataGridView

[英]Refresh DataGridView After INSERT INTO Database

我想在数据库表中的每个INSERT INTO之后添加刷新DataGridView

OleDbConnection objConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data    Source='database.accdb'");
String sqlquery = "INSERT INTO MyTable" +
            "(Column1, Column2, Column3, Column4)" +
            "VALUES (@Column1, @Column2, @Column3, @Column4)";

OleDbCommand objCommand = new OleDbCommand(sqlquery, objConnection);
objConnection.Open();

objCommand.Parameters.AddWithValue("@Column1", txtAccount.Text);
objCommand.Parameters.AddWithValue("@Column2", txtAccountNumber.Text);
objCommand.Parameters.AddWithValue("@Column3", txtCardNumber.Text);
objCommand.Parameters.AddWithValue("@Column4", txtDescription.Text);

objCommand.ExecuteNonQuery();

objConnection.Close();

// Now I want Refresh Data Grid View
BindingSource bindingsource = new BindingSource();
bindingsource.DataSource = this.databaseDataSet.MyTable;

dataGridView1.DataSource = bindingsource;
dataGridView1.Update();
dataGridView1.Refresh();

但是它不会刷新数据网格视图。

我该如何解决?

绑定新源时,请尝试此操作,

dataGridView1.DataSource = null;
dataGridView1.DataSource = bindingsource;
dataGridView1.Refresh();

你可以这样尝试

private void AddData()
{
    if(command.ExecuteNonQuery()>0)
    {
         //Call your BindData method to reflect the latest records on datagridview when binding
         BindData();
    }
}
private void BindData()
{
   //Bind your datagridview with the datasource
}

您实际上不需要刷新任何东西-只需将新对象添加到数据库并更新绑定源即可。

这是一个例子。 我有一个带有dataGridView,docsBindingSource和addButton的窗体。 Form1_Load-非常重要的部分(由于绑定)

private void Form1_Load(object sender, EventArgs e)
    {
        using (var db = new db())
        {
            // now our dataGridView will show us 'docs'
            docsBindingSource.DataSource = db.docs.ToList();
        }

    }

// a.k.a. Insert event
private async void addBtn_Click(object sender, EventArgs e)
    {
        using (frmAddEdit frm = new frmAddEdit())
        {
            frm.ShowDialog();

            // after I Show frmAddEdit Form, i will change his DialogResult
            // in some period of time
            if (frm.DialogResult == DialogResult.OK)
            {
                try
                {
                    // then i make db object
                    using (var db = new db())
                    {
                        // adding data
                        var added = db.docs.Add(new docs()
                        {
                            docsAgent = frm.docsInfo.docsAgent,
                            docsStaff = frm.docsInfo.docsStaff,
                            docsType = frm.docsInfo.docsType
                        });

                        // saving
                        await db.SaveChangesAsync();

                        // and updating my bindingSource, which is a source for
                        // my dataGridView
                        docsBindingSource.Add(added);

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    throw;
                }
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM