简体   繁体   中英

Refresh DataGridView After INSERT INTO Database

I want add refresh DataGridView after every INSERT INTO in database table

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

But it doesn't refresh the data grid view.

How can I fix that?

Try this when you binding new source,

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

You may try like this,

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
}

You really don't need to refresh anything - just add your new object to DB and update binding source.

Here is an example. I have a Form with dataGridView, docsBindingSource and addButton. Form1_Load - is very important part(because of binding)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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