DataGridViewRow.ReadOnly not active

0
0

Hi Wisej,

in Window.Load set DataGridViewRow.ReadOnly=True. But It doesn’t active.

Can you check it?

Thank you.

  • You must to post comments
0
0

When adding/changing a data source while loading (before the container is created and visible) the BindingContextChanged event is fired multiple times (when the parent changes, when the control is created which happens when made visible, etc.) In your case when , Windows6 (mdi child) is created second, it’s created hidden and then made visible when it becomes active so you get the population twice. When created first it’s created already visible. That’s why there is the DataBindingComplete event to handle the data-bound rows. You get a similar behavior in winforms.

The binding context is important to sync all the controls sharing the same data source.

I also tried

Private Sub DataGridView1_RowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
Me.DataGridView1.Rows(e.RowIndex).ReadOnly = True
End Sub

The issue you see is that your winforms code assumes that e.RowCount is always 1, while Wisej adds the data rows in bulk and fires RowsAdded with e.RowCount = (count) so you should do this:

Private Sub DataGridView1_RowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
For i = 0 To e.RowCount – 1
Me.DataGridView1.Rows(e.RowIndex + i).ReadOnly = True
Next
End Sub

In any case, DataBindingComplete is a better event to handle.

 

  • You must to post comments
0
0

Hi Huynh,

thanks for reporting this.

In your case the best way to fix the problem is to move the disabling part of the rows into
a handler for the DataBindingComplete event.

Otherwise when only disabling in the Load event you risk that the BindingContextChanged event
is fired again and the rows might get recreated and then be enabled.

The same problem can also happen in WinForms so the safest bet is to rely on the DataBindingComplete event.

Best regards
Frank

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.