Results 1 to 7 of 7

Thread: Pick from DataGridView ComboBox with Pre-Filled Collection

  1. #1
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2011
    Posts
    43
    Reputation
    97

    Thumbs up Pick from DataGridView ComboBox with Pre-Filled Collection

    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:
    Code:
        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 by Runescope; 09-28-2018 at 1:18 PM. Reason: Resolved

  2. #2
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2011
    Posts
    43
    Reputation
    97
    I've changed it to this,

    Code:
        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 by Runescope; 09-27-2018 at 7:16 PM.

  3. #3
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,998
    Reputation
    1728
    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.

  4. #4
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2011
    Posts
    43
    Reputation
    97
    Quote Originally Posted by jmcilhinney View Post
    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?

  5. #5
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,998
    Reputation
    1728
    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.

  6. #6
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2011
    Posts
    43
    Reputation
    97
    Quote Originally Posted by jmcilhinney View Post
    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.

  7. #7
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2011
    Posts
    43
    Reputation
    97
    Okay. so I figured this out.

    Here is the updated code:

    Code:
        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.

Similar Threads

  1. Question ComboBox not filled with DataTable - Why?
    By VBobCat in forum Windows Forms
    Replies: 10
    Last Post: 12-16-2011, 10:44 PM
  2. Question Combobox databound filled question
    By zendog1960 in forum Windows Forms
    Replies: 0
    Last Post: 07-20-2010, 12:25 PM
  3. ComboBox and collection
    By FatEric in forum Database General Discussion
    Replies: 2
    Last Post: 05-08-2008, 6:01 AM
  4. combobox selectedindexchanged fires when filled with dataset
    By Ultrawhack in forum Listviews / Treeviews
    Replies: 1
    Last Post: 11-09-2006, 7:59 PM
  5. How to clear combobox item collection
    By tsh in forum Database General Discussion
    Replies: 9
    Last Post: 01-20-2006, 1:46 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •