0

I have multiple runtime generated datagridview controls and I would like to be able to see if the user has selected multiple rows in a specific datagridview. For some reason, the result of the following is always zero. When I F9 stop the program, I can see that the selected value of this datagridview row is false. Any ideas how to fix this?

My code is as follows:

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

Thanks

I have now tried the following:

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

As well as:

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

But it still returns a zero row count.

J2Tuner
  • 411
  • 1
  • 5
  • 17

2 Answers2

1

This should be what you are looking for:

dgv.SelectedRows.Count
WozzeC
  • 2,630
  • 1
  • 13
  • 12
  • This code is in a module separate from the form, and the datagridview control is generated at runtime, so I have to search through the collection of controls first to find which datagridview is selected, and then try to find the selected rows. At this point, the selected rows keeps returning zero, and I am seeing that specific row's selected property as false when I F9 stop the program. I am wondering if my code will only see the design time created datagridview control. – J2Tuner Dec 07 '12 at 15:00
  • It is not giving me SelectedRows as a method of dgv. Is there another way to get to this method? When I look at the Selected Row count when the program is paused, I can see that even though I have selected multiple rows in that specific datagridview, the count is still zero. – J2Tuner Dec 07 '12 at 15:15
  • That might be because dgv is a Control. Try Dim tempDGV as DataGridView = CType(dgv, DataGridView) if the Name is a match. then Do tempDGV.SelectedRows. I find it odd that it wouldn't work. – WozzeC Dec 07 '12 at 15:25
  • I think I see the issue, but I don't know how to get there. The code you have suggested brings me back to where I started. I did have `For Each dgv As DataGridView In tbp.Controls`, but it gives me an exception for each control it sees that is not a DataGridView. How can I check to see what the control type is before I cast it as a DataGridView? – J2Tuner Dec 07 '12 at 16:06
  • Just Put the ctype row inside the if statement where you check the name. The matching Control should be type castable. – WozzeC Dec 07 '12 at 16:29
1

Ok, so the solution is as follows:

Both:

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

And:

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

Work as they should. The problem was that in my runtime generated DataGridView controls, I did not set the SelectionMode Method to FullRowSelect, and I was selecting individual cells, which do not count as "Rows".

Thanks for your help!

J2Tuner
  • 411
  • 1
  • 5
  • 17