简体   繁体   中英

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?

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

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