I wonder what I need to do in order to make the DataGridView behave as expected:
I do have a List<ioChannel>
(named myList) of objects of this type:
public class ioChannel
{
public string Name { get; set; }
public int Id { get; set; }
}
I tried to bind the DataGridView to this list:
bs = new BindingSource { DataSource = myList };
dgvChannels.DataSource = bs;
Now I created a button that adds elements to myList:
private void btAddChannel_Click(object sender, EventArgs e)
{
myList.Add(new ioChannel() { Id = myList.Count, Name = "My Channel" });
}
When I press the button, my List gets populated with new ioChannel
's, but the dgvChannels
does not add any rows. What do I need to do in order to make this happen?
BTW: When myList
gets populated with some ioChannel
´s before assigning the BindingSource as DataSource, the get shown properly in the grid.
As @Loathing commented BindingList worked for me too. How it will work :
BindingList<ioChannel> myList = new BindingList<ioChannel>();
dgvChannels.DataSource = myList;
BindingList has event that notifies changes in list. So that datagridview can notified to redraw itself, check this. For more information visit msdn.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With