如何在多个运行时生成的datagridview控件中正确使用GetRowCount方法?

时间:2012-12-07 14:34:58

标签: vb.net datagridview datagridviewrow

我有多个运行时生成的datagridview控件,我希望能够看到用户是否在特定的datagridview中选择了多行。由于某种原因,以下结果始终为零。当我F9停止程序时,我可以看到此datagridview行的选定值为false。任何想法如何解决这个问题?

我的代码如下:

strDGVName = "dgvCheckpoint" & intTimeModificationSender

For Each tbp As TabPage In frmTimingP2P.tabctrlTimingTable.Controls
    For Each dgv In tbp.Controls
        If dgv.Name = strDGVName Then
            intSelectedRowCount = dgv.Rows.GetRowCount(DataGridViewElementStates.Selected)
        End If
    Next
Next

由于

我现在尝试了以下内容:

For Each tbp As TabPage In frmTimingP2P.tabctrlTimingTable.Controls
    For Each ctrl As Control In tbp.Controls
        Dim dgv As DataGridView = TryCast(ctrl, DataGridView)
        If Not dgv Is Nothing Then
            If dgv.Name = strDGVName Then
                intSelectedRowCount = dgv.SelectedRows.Count
            End If
        End If
    Next
Next

以及:

Dim c As Collections.Generic.IEnumerable(Of DataGridView)
For p = 0 To frmTimingP2P.tabctrlTimingTable.TabCount - 1
    c = frmTimingP2P.tabctrlTimingTable.TabPages(p).Controls.OfType(Of DataGridView)()
    If c(0).Name = strDGVName Then p = frmTimingP2P.tabctrlTimingTable.TabCount
    End If
Next
intSelectedRowCount = c(0).SelectedRows.Count

但它仍然会返回零行数。

2 个答案:

答案 0 :(得分:1)

这应该是你要找的东西:

dgv.SelectedRows.Count

答案 1 :(得分:1)

好的,所以解决方案如下:

这两种:

For Each tbp As TabPage In frmTimingP2P.tabctrlTimingTable.Controls
    For Each ctrl As Control In tbp.Controls
        Dim dgv As DataGridView = TryCast(ctrl, DataGridView)
        If Not dgv Is Nothing Then
            If dgv.Name = strDGVName Then
                intSelectedRowCount = dgv.SelectedRows.Count
            End If
        End If
    Next
Next

Dim c As Collections.Generic.IEnumerable(Of DataGridView)
For p = 0 To frmTimingP2P.tabctrlTimingTable.TabCount - 1
    c = frmTimingP2P.tabctrlTimingTable.TabPages(p).Controls.OfType(Of DataGridView)()
    If c(0).Name = strDGVName Then p = frmTimingP2P.tabctrlTimingTable.TabCount
Next
intSelectedRowCount = c(0).SelectedRows.Count

按他们的意愿工作。问题是在我的运行时生成的DataGridView控件中,我没有将SelectionMode方法设置为FullRowSelect,而我选择的单个单元格不算“行”。

感谢您的帮助!