I have several problems with rows in a DataGridView. I posted them yesterday to
http://stackoverflow.com/questions/1207324/datagridview-vb-net-problems-nonvisible-rows-are-still-visible-and-the-datagri but as I have got no answer yet, I give it a try here as well! When I get an answer at either place, I'll inform about that at the other place.
Background information: The DataGridView (DataGridViewCalib) is in one TabPage of a TabControl. Some columns of the DataGridView are automatically DataGridViewCheckBoxColumn as the DataSource have some colums which are Boolean. This is a Windows Form written in VB.Net with Visual Studio 2008. The user loads an input data file.
The problems: 1) At first arrival in the TabPage, ShowDataGridViewCalib (code below) is called. All rows are then shown in the DataGridView, despite the code saying that some rows should not be visible. Breakpoints in the code show that the code do arrive at the
Rows.Visible = False
events.
Despite all rows beeing shown a Watch in the debugger shows that:
DataGridViewCalib.DisplayedColumnCount(True)=0
DataGridViewCalib.DisplayedColumnCount(False=0)
DataGridViewCalib.DisplayedRowCount(True)=0
DataGridViewCalib.DisplayedRowCount(False)=0
But
Columns.Visible=False
works as expected.
When running the subroutine ShowDataGridViewCalib a second time, by enforcing it from the checkbox CbUniform, the reduction in the number of rows works as it should, and the DataGridViewCalib.Displayed...Count is correct.
What causes the whole DataTable to be shown the first time?
2) The user can load another input data file. When a second input file is loaded and ShowDataGridViewCalib is run, another strange thing occur. Watch in the debugger says DataGridViewCalib.DataSource = {System.Data.DataTable} and this DataTable has the same properties as dtCatchCalib,
but
DataGridViewCalib.Columns.Count = 0
DataGridViewCalib.Rows.Count = 0
and nothing is shown in the DataGridView.
Before the second input file is loaded most of the data is cleared, including
DataGridViewCalib.Columns.Clear()
dtCatchCalib.Clear()
Especially for this second problem I assume that the error might be somewhere outside of ShowDataGridViewCalib, but I would be very happy for hints about what causes the DataGridView to have a DataSource (with several rows and columns) but still no rows and columns.
The code:
Private Sub ShowDataGridViewCalib()
'[...]
Dim kolwidth As Integer = 77
DataGridViewCalib.DataSource = dtCatchCalib
DataGridViewCalib.Refresh()
Dim kol As DataGridViewColumn
For Each kol In DataGridViewCalib.Columns
kol.SortMode = DataGridViewColumnSortMode.NotSortable
If CbUniform.Checked = False Then
kol.Visible = True
kol.Width = kolwidth
If kol.Name = "CatchmentID" Then
kol.ReadOnly = True
ElseIf kol.Name = "parc0" Then
kol.HeaderText = "c0"
ElseIf kol.Name = "statr" Then
kol.Visible = False
kol.ReadOnly = True
kol.HeaderText = "r"
End If
Else
If kol.Name = "IncludeObs" Then
kol.Width = kolwidth
ElseIf kol.Name = "CatchmentID" Then
kol.ReadOnly = True
kol.Width = kolwidth
Else
kol.Visible = False
End If
End If
Next
'Dim rad As DataGridViewRow
'Dim dum As Integer
'dum = 0
'For Each rad In DataGridViewCalib.Rows
' dum += 1 ' # rows in dtCatchCalib is = # subcatchments
' DataGridViewCalib.CurrentCell = Nothing ' Unselect the current cell, needed to be able to set the row invisible
' rad.Visible = False ' TEST
' If ObsLst(dum) = True Then ' ObsLst have its first value at index 1
' rad.Visible = True
' Else
' DataGridViewCalib.CurrentCell = Nothing ' Unselect the current cell, needed to be able to set the row invisible
' rad.Visible = False
' End If
'Next
For i = 0 To dtCatchCalib.Rows.Count - 1
DataGridViewCalib.CurrentCell = Nothing
DataGridViewCalib.Rows(i).Visible = False
If ObsLst(i+1) = True Then ' ObsLst have its first value at index 1
DataGridViewCalib.Rows(i).Visible = True
Else
DataGridViewCalib.CurrentCell = Nothing
DataGridViewCalib.Rows(i).Visible = False
End If
Next
'[...]
End Sub
There are two alternative ways of handling the rows in the code. The first attempt (commented away here) is probably the "nicest".