I do not know how to do this if the DataGridView raises an event when its DataSource is programmatically changed - this is by design.
The best way I can satisfy your requirements is to inject a BindingSource into the mix. - Binding sources do increment events when their DataSource changes.
Something like this works (you will obviously need to fine-tune it to your needs):
bindingSource1.DataSource = tbData; dataGridView1.DataSource = bindingSource1; bindingSource1.ListChanged += new ListChangedEventHandler(bindingSource1_ListChanged); public void bindingSource1_ListChanged(object sender, ListChangedEventArgs e) { DataGridViewCellStyle cellStyle = new DataGridViewCellStyle(); cellStyle.ForeColor = Color.Red; dataGridView1.Rows[e.NewIndex].Cells[e.PropertyDescriptor.Name].Style = cellStyle; }
Another option is to do this by subscribing directly to the data - if it is a BindingList, it will dispatch NotifyPropertyChanged events using its own ListChanged event. In a more MVVM scenario, which might be cleaner, but in WinForms, BindingSource is probably best.
source share