0

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).

Nepaluz
  • 669
  • 1
  • 12
  • 27
  • Remove row from `DataTable`. – Fabio Jan 12 '17 at 12:47
  • 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 Jan 12 '17 at 13:00
  • If you make row hidden in datagridview - how you then can change cell value back to `N`? – Fabio Jan 12 '17 at 13:01
  • Like I clearly state in the question, I **programmatically** change the value (read the last paragraph). – Nepaluz Jan 12 '17 at 13:03
  • How is the user going to be able to set anything to anything on a row that is invisible? – Ňɏssa Pøngjǣrdenlarp Jan 12 '17 at 13:11
  • For what it is worth, the datagridview in this scenario itself is ReadOnly and only programmatically updated through the datatable by routines that have user input elsewhere. – Nepaluz Jan 12 '17 at 13:17
  • That event is only fired if the value of a cell is changed. It's confusing to understand _how_ you want to achieve this because we have no understanding of how your program works. Do you set the values to Y or N through another routine? If so then why don't you look at setting the visibility then. – Bugs Jan 12 '17 at 13:20
  • The cell value is changed, that's for sure, but the event never fires! And yes, values are changed by other routines in the bound datatable. Good question why I don't set the visibility when I update the values in the underlying table. The answer is speed, resource usage (having to repeatedly suspend and resume the CurrencyManager on the grid) and / else errors updating from a non UI thread. – Nepaluz Jan 12 '17 at 13:29

3 Answers3

1

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.

John0987
  • 63
  • 5
0

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.

minimalist
  • 87
  • 4
  • Yes, look at my original question (and the edit) which makes it clear that directly updating the control works but is very slow on top of requiring to suspend binding et al, whereas updating the datatable is extremely fast, however the visibility of the row is not toggled. – Nepaluz Jan 12 '17 at 16:41
0

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.

Nepaluz
  • 669
  • 1
  • 12
  • 27