Refresh Datagridview When Updating Data Source

Refresh DataGridView when updating data source

Well, it doesn't get much better than that. Officially, you should use

dataGridView1.DataSource = typeof(List); 
dataGridView1.DataSource = itemStates;

It's still a "clear/reset source" kind of solution, but I have yet to find anything else that would reliably refresh the DGV data source.

C# refresh DataGridView when updating or inserted on another form

// Form A
public void loaddata()
{
//do what you do in load data in order to update data in datagrid
}

then on Form B define:

// Form B
FormA obj = (FormA)Application.OpenForms["FormA"];

private void button1_Click(object sender, EventArgs e)
{
obj.loaddata();
datagridview1.Update();
datagridview1.Refresh();
}

How to refresh the DataSource on a WinForms DataGridView?

The answer is to have the gridview connected to the BindingList rather than the List.

Refresh a DataGridView on update to its list data source

Re assign grid source to updated list of objects and call databind method again.

How to refresh the data grid view when update? Window from C#

When you call the database_connect method, pass the form reference as below with this,

private void button1_Click(object sender, EventArgs e)
{
//Get textfield data....

//If user doesn't input the item data, show the error message. Else, update to database.
if (error == true)
{
error_msg_form emf = new error_msg_form();
emf.Show();
}
else
{
//Form1 f1 = new Form1();
database_function df = new database_function();
df.database_connect(this, item_code_tb.Text, des_tb.Text, unit_tb.Text, Convert.ToDouble(unit_price_tb.Text));
//f1.refresh_dataGridView();
}
}

Then, add the parameter Form1 form1 in the method as below and call form1.refresh_dataGridView() using the same reference.

public void database_connect(Form1 form1, String item_code, String des, String unit, double price)
{
//Form1 f1 = new Form1();
try
{

connect.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connect;
command.CommandText = "INSERT into item (item_code, description, unit, price) values ('" + item_code + "', '" + des + "', '" + unit + "', " + price + ")";
command.ExecuteNonQuery();

connect.Close();
}
catch(Exception e)
{
Debug.WriteLine(e.Source);
connect.Close();
}
form1.refresh_dataGridView();
}

Also, change the code as below to refresh the data grid view,

public void refresh_dataGridView()
{
dataGridView1.DataSource = typeof(List);
dataGridView1.DataSource = df.get_view();
Debug.WriteLine("refuesh");
}

Refresh datagridview after update data in another form

Form 2 Button Click use this Code

private void button1_Click(object sender, EventArgs e)
{
Form frm=(Form)this.MdiParent;
DataGridView dt = (DataGridView)frm.Controls["dataGridView1"];
dt.Rows[0].Cells[0].Value = textBox1.Text;

}

Datagridview doesn't refresh even after reassigning its datasource to bindingsource

I found the solution to my problem. I did not need to handle the BindingContext_Changed event, and the general methodology of:

bindingSource1.ResetBindings(false);
dataGridView1.DataSource = null;
bindingSource1.DataSource = null;
bindingSource1.DataSource = mytable;
dataGridView1.DataSource = bindingSource1;

was the correct way to update the grid.

The real problem: From my original question: "In case this might be relevant, my datagridviews are part of a panel whose parent is a splitcontainer." The following line of code:

 split.Panel2.Controls.Add(new ConfigurableMatrices(comboBox1.SelectedItem.ToString(), comboBox2.SelectedItem.ToString())); 

was re-adding this class (ConfigurableMatrices, which is the one which has my datagridviews in it) with all its controls to the second panel of the splitcontainer parent. This line of code is executed from the other half of the splitcontainer. I was also refreshing after that:

split.Panel2.Controls.Add(new ConfigurableMatrices(comboBox1.SelectedItem.ToString(),  
comboBox2.SelectedItem.ToString()));
split.Panel2.Refresh();

However this way, I was only adding a new class (ConfigurableMatrices) each time on top, and this didn't for some reason correctly update. The solution which works for me right now is to call the Dispose method just before the above two lines of code:

foreach (Control control in split.Panel2.Controls)
{
control.Dispose();
}
split.Panel2.Controls.Add(new ConfigurableMatrices(comboBox1.SelectedItem.ToString(), comboBox2.SelectedItem.ToString()));
split.Panel2.Refresh();

I'm hoping the Dispose method is the correct way to tackle this and won't trigger other problems in the future.



Related Topics



Leave a reply



Submit