MojtabaSh
MojtabaSh

Reputation: 637

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?

Upvotes: 0

Views: 5885

Answers (3)

Nikolay Gogol
Nikolay Gogol

Reputation: 999

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

Upvotes: 0

Shyju
Shyju

Reputation: 203

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
}

Upvotes: 0

iJay
iJay

Reputation: 4293

Try this when you binding new source,

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

Upvotes: 1

Related Questions