【发布时间】:2010-11-15 11:49:20
【问题描述】:
DataGridView 中的行有几个问题。
背景信息: DataGridView (DataGridViewCalib) 在TabControl 的一个TabPage 中,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
有两种方法可以处理代码中的行。第一次尝试(在此处注释掉)可能是“最好的”。
这是我第一次在编程论坛上发布问题。如果我表达不够清楚,请再问我一次。
【问题讨论】:
-
我刚刚在social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/… 在这里发布了同样的问题@ 当我在一个论坛上得到答案时,我会在另一个论坛上提供指向该问题的链接。
标签: vb.net datagridview datasource rows