0
votes

I have a Datagridview bound to a datatable. I'd like to hide or show a row depending on the value of a cell, for example if the cell value is "N" make the row visible else if it is "Y", hide the row.

The datacolumn is setup as:

New DataColumn With {.ColumnName = "Rec", .AllowDBNull = False, .DefaultValue = "N", .DataType = GetType(String)}

I have also handled the CellValueChanged event of the Datagridview as below:

Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
    If CType(sender, DataGridView).Columns(e.ColumnIndex).HeaderText.Equals("Rec") Then
        CType(sender, DataGridView).Rows(e.RowIndex).Visible = CType(sender, DataGridView).Item(e.ColumnIndex, e.RowIndex).Value.Equals("N")
    End If
End Sub

But when I programmatically change the value to "Y" (and I can see the value has changed in the grid), the row is still visible. I also put a break inside the event handler and it is NOT fired! So the question, how do I hide a datagridviewrow when I change a cellvalue to "Y"?

EDIT

Just to add some more clarity to the problem at hand, this is how I programmatically update the grid's datasource:

CType(DataGridView1.DataSource, DataTable).Item("Rec") = "Y"

I can directly update the grid and therefore fire the cellvaluechanged event, however this throws the CurrencyManager error, and therefore having to suspend binding then resuming binding to the grid every time the cell value updates. That works but at a snail's pace for even a small dataset (even with double buffering implemented via reflection).

3
Remove row from DataTable.Fabio
If I remove the row from the datatable, it will no longer be possible to update the row to make it visible again in the datagridview.Nepaluz
If you make row hidden in datagridview - how you then can change cell value back to N?Fabio
Like I clearly state in the question, I programmatically change the value (read the last paragraph).Nepaluz
How is the user going to be able to set anything to anything on a row that is invisible?Ňɏssa Pøngjǣrdenlarp

3 Answers

1
votes

Instead of binding directly to the DataTable, try binding the DataTable to a BindingSource (control) and then binding that to your grid. Then you can use the events in the BindingSource to check when the data has changed, either from a change in the grid or a programmatic change directly to the data table.

Alternatively, there are events fired by the DataTable that you can hook into to determine when the data has changed.

0
votes

O.K when you update the cellvalue you extract the rowindex from the upgraded cell and in the next step you hide the cell with, some thing like:

 DataGridView1.Rows.Item(rowindex).Visible = False

So for a test I did this:

DataGridView1(1, 2).Value = "Y"
DataGridView1.Rows.Item(2).Visible = False

and it works.

0
votes

Right, I resolved this by partly using the suggestion by @John0987 as the issue I was facing seems to have been a bit more complex than I let on. Basically, it is a hack where I initialise up a form wide Boolean variable to false and handle the DataGridView's DataBindingComplete event which sets the Boolean to true and added a Boolean check in the CellValueChanged event before making the row invisible. This, so far, works flawlessly.