Resolved Pick from DataGridView ComboBox with Pre-Filled Collection

Runescope

Well-known member
Joined
Jan 6, 2011
Messages
53
Programming Experience
Beginner
So I have a DataGridView control, it has a few columns, one column is a DataGridViewComboBoxColumn. I have pre-filled it through it's own Items collection with the numbers 0 to 9. I want to pick one of the numbers as a default when the program loads, how do I select it's index?

Here's the code I'm using:
VB.NET:
    Public Sub FillMeetingMembers()
        Dim MListRowVals(6) As Object
        Dim MGID, MemberName As String
        
        grdRoleCall.Rows.Clear()
        For idx As Integer = 0 To MemberDT.Rows.Count - 1
            With MemberDT.Rows(idx)

                MGID = .Item("GID")
                If IsDBNull(.Item("Member")) = True Then MemberName = " " Else MemberName = .Item("Member")

                MListRowVals(0) = MemberName
                MListRowVals(1) = iCons.Images(19)
                MListRowVals(2) = iCons.Images(19)
                MListRowVals(3) = 0 ' <-- This is the line I want to set.
                MListRowVals(4) = MGID
                MListRowVals(5) = 0
                grdRoleCall.Rows.Add(MListRowVals)
            End With
        Next
    End Sub

I've never used a combox in a datagrid before, so you'll have to be fairly explicit and simple in your explanations, but any help would be appreciated.
 
Last edited:
I've changed it to this,

VB.NET:
    Public Sub FillMeetingMembers()
        Dim MListRowVals(6) As Object
        Dim MGID, MemberName As String
        Dim cbCol As New DataGridViewComboBoxColumn ' <- Added this line

        grdRoleCall.Rows.Clear()
        For idx As Integer = 0 To MemberDT.Rows.Count - 1
            With MemberDT.Rows(idx)

                MGID = .Item("GID")
                If IsDBNull(.Item("Member")) = True Then MemberName = " " Else MemberName = .Item("Member")

                MListRowVals(0) = MemberName
                MListRowVals(1) = iCons.Images(19)
                MListRowVals(2) = iCons.Images(19)
                MListRowVals(3) = cbCol.Items(1) ' <- Changed this line, still getting an error
                MListRowVals(4) = MGID
                MListRowVals(5) = 0
                grdRoleCall.Rows.Add(MListRowVals)
            End With
        Next
    End Sub

I'm getting the error: "InvalidArgument=Value of '1' is not valid for 'index'.". Which in my mind is screwy as it's pre-populated with 10 things with an index of 0 to 9, so it should work.
 
Last edited:
In that second code snippet, the issue is that you are creating a new combo box column in code right there and trying to use its Items collection, which you have never added anything to. You need to use the column that is already in the grid because that's the one you added the items too and that's the one whose cell values you want to set.
 
In that second code snippet, the issue is that you are creating a new combo box column in code right there and trying to use its Items collection, which you have never added anything to. You need to use the column that is already in the grid because that's the one you added the items too and that's the one whose cell values you want to set.

Yeah, I figured that out after a few minutes. But I'm still left with the problem of How?
 
If you added the column in the designer then there should be a member variable for it, just as there is for controls you add in the designer. Alternatively, you can always get any column from the Columns collection of the grid.
 
If you added the column in the designer then there should be a member variable for it, just as there is for controls you add in the designer. Alternatively, you can always get any column from the Columns collection of the grid.

Sorry if this is going to sound snarky, but I've been frustrated with this for a couple of days now. I know WHAT I want to do, I don't know HOW to do it. People have suggested theory, but no practical help. I've googled the heck out of it and can't find anything.
 
Okay. so I figured this out.

Here is the updated code:

VB.NET:
    Public Sub FillMeetingMembers()
        Dim MListRowVals(6) As Object
        Dim MGID, MemberName As String
        Dim cbCol As DataGridViewComboBoxCell

        grdRoleCall.Rows.Clear()
        For idx As Integer = 0 To MemberDT.Rows.Count - 1
            With MemberDT.Rows(idx)

                MGID = .Item("GID")
                If IsDBNull(.Item("Member")) = True Then MemberName = " " Else MemberName = .Item("Member")

                MListRowVals(0) = MemberName
                MListRowVals(1) = iCons.Images(19)
                MListRowVals(2) = iCons.Images(19)
                MListRowVals(4) = MGID
                MListRowVals(5) = 0
                grdRoleCall.Rows.Add(MListRowVals)
                cbCol = DirectCast(grdRoleCall(3, idx), DataGridViewComboBoxCell)
                cbCol.Value = cbCol.Items(0)
            End With
        Next
    End Sub

I couldn't find a way to set that column before the row was added the datagridview, so I had to do it afterwards.

BTW, in order to read from it you would do sort of the same thing. Using 'GuestNum = cbCol.FormattedValue' gets the displayed information, not the index, though I'm sure there's a way to do that too, but for me, this worked as the displayed number is the same as the index number.

I hope this helps anyone else that is having a similar problem.
 
Back
Top