Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView bound to List does not reflect changes when adding objects to the List

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.

like image 915
Rob Avatar asked Oct 14 '25 08:10

Rob


1 Answers

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.

like image 119
Haider Ali Wajihi Avatar answered Oct 16 '25 20:10

Haider Ali Wajihi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!