Search code examples
c#datagridviewbindingsource

Programmatically modify cells in a filtered datagridview


If I load up a DataGridView from a datasource,

this.exampleTableAdapter.Fill(this.refdataDataSet.Example);

Apply a filter via the Binding Source

exampleBindingSource.Filter = "TrackingId = 'x'";

And then attempt to iterate over the filtered DataGridView and modify them

foreach (DataGridViewRow row in datagridExampleList.Rows)
{
   //Tried to set rows directly
   row.Cells["TrackingId"].Value = "";
   //Also tried variations of setting the DataBoundItem directly
}

However, it will not iterate over the entire collection for some reason.

When I debug I get the Rows collection returned (filtered with x items matching the filter), but as I change the values in the collection (specifically the values that are the target of the filter) the collection changes on the fly and the iteration misses items even when the filter is suspended and list change events have been set to false.

Nothing seems to work..

Any suggestions ?


Solution

  • Following code must let you change all rows.

                var filter = exampleBindingSource.Filter;
                exampleBindingSource.Filter = null;
                foreach (DataGridViewRow row in datagridExampleList.Rows)
                {
                    //Tried to set rows directly
                    row.Cells["TrackingId"].Value = "";
                    //Also tried variations of setting the DataBoundItem directly
                }
                exampleBindingSource.Filter = filter;