我在 a 中的行有几个问题DataGridView
。
背景信息: ( DataGridView
)DataGridViewCalib
在其中之一TabPage
中TabControl
,DataGridView 的某些列自动DataGridViewCheckBoxColumn
作为DataSource
具有某些列的列Boolean
。这是使用 Visual Studio 2008 用 VB.Net 编写的 Windows 窗体。用户加载输入数据文件。
问题:
1)在第一次到达时TabPage
,ShowDataGridViewCalib
(下面的代码)被调用。然后所有行都显示在 中DataGridView
,尽管代码说某些行不应该是可见的。代码中的断点表明代码确实到达了Rows.Visible = False
事件。尽管在调试器中显示了所有行,但 Watch 显示:
DataGridViewCalib.DisplayedColumnCount(True)=0
DataGridViewCalib.DisplayedColumnCount(False=0)
DataGridViewCalib.DisplayedRowCount(True)=0
DataGridViewCalib.DisplayedRowCount(False)=0
Columns.Visible=False
按预期工作。
第二次运行子例程ShowDataGridViewCalib
时,通过从 强制执行它checkbox
CbUniform
,行数的减少可以正常工作,并且DataGridViewCalib.Displayed...Count
是正确的。
是什么导致整体DataTable
第一次显示?
2) 用户可以加载另一个输入数据文件。当第二个输入文件被加载并ShowDataGridViewCalib
运行时,另一个奇怪的事情发生了。DataGridViewCalib.DataSource = {System.Data.DataTable}
这与DataTable
具有相同的属性dtCatchCalib
,但是
DataGridViewCalib.Columns.Count = 0
DataGridViewCalib.Rows.Count = 0
中没有显示任何内容DataGridView
。在加载第二个输入文件之前,大部分数据都会被清除,包括DataGridViewCalib.Columns.Clear()
和dtCatchCalib.Clear()
. 特别是对于第二个问题,我假设错误可能在 之外的某个地方ShowDataGridViewCalib
,但是我很乐意提供有关导致DataGridView
有DataSource
但仍然没有行和列的原因的提示。
编码:
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
有两种方法可以处理代码中的行。第一次尝试(在此处注释掉)可能是“最好的”。
这是我第一次在编程论坛上发布问题。请再问我是否表达得不够清楚。