1
votes

I know this is an old problem... I have DataGridView that's bound directly to a generic List of custom objects. This is the event when the user clicks the "Add new row" button:

private void btnAddStation_Click(object sender, EventArgs e)
{
    if (dgvStationConfiguration.DataSource != null && ((List<StationConfiguration>)dgvStationConfiguration.DataSource).Count > 0)
    {
        var stations = (List<StationConfiguration>)dgvStationConfiguration.DataSource;
        stations.Add(new StationConfiguration(string.Empty, string.Empty, string.Empty, stations.Last().FolderName));
        IsGridDirty = true;
    }
}

but the new row doesn't show up. In the debugger, I see it's been added to the datasource, but I cannot force it to show.

I've tried the old hack of setting the datasource to null and back to the list again, but it messes up the order of my datagridview's columns.

I've also tried Refresh and EndEdit with no success.

1
Try using a BindingList instead of a List. - LarsTech

1 Answers

1
votes

The List<T> collection does not support the IBindingList interface, so in order for your DataGridView control to be notified that an item in the collection has been removed or added, try using a BindingList<T> collection instead.