Add row to DataGrid with DataSource defined

3

I have DataGridView that has DataSource setado with List<MinhaClasse> and would like to add a new line to it.

I know this is not possible using the AddRow() method of DataGrid .

Is there any way I can do this?

    
asked by anonymous 16.10.2015 / 21:53

1 answer

3

You can do this by setting a BindindList<> as DataSource . Thus, whenever an item is added from BindingList<> it will automatically be inserted into DataGridView .

var source = new BindingList<MinhaClasse>();
dataGridView1.DataSource = source;
source.Add(new MinhaClasse { Nome = "João", Idade = 32 });    
    
16.10.2015 / 22:07