Thijser Thijser - 2 months ago 11
C# Question

C# datagridview adding extra items not showing up

I have a

DataGridView
and a
button
. On pressing this
button
it should add in a new row into the
DataGridView
. In order to do this I have the following code:

List<NavigationInfo> navigationRules = new List<NavigationInfo>();
private void button1_Click(object sender, EventArgs e)
{
navigationRules.Add(new NavigationInfo());
setDataSource(navigationRules);
}


public void setDataSource(List<NavigationInfo> data)
{
this.dataGridView2.DataSource = data;
this.dataGridView2.Show();
this.dataGridView2.Invalidate();
this.dataGridView2.Update();
}


Now when I click on this button the first time it correctly adds in a new row. If however I click on it a second time it does not show up.
Breakpointing shows me that the
datasource
does actually receive extra items. They just don't show up.

Anybody know how to fix this?

Answer

You need to call refresh() once you update, also set the DataSource to null,

this.dataGridView2.DataSource = null;
this.dataGridView2.DataSource = data;        
dataGridView2.update();
dataGridView2.refresh();  
Comments