none
Loading unbound combo box in datagridview

    Question

  • I have a datagridview that has bound fields and an unbound combo box.  I need to load the contents of the combo box with different values based on the value of a bound cell.  When doing this, the combo box on every row has the last values loaded no matter the value of the cell that determines what to load.  Any suggestions?  Thanks

    For Each row As DataGridViewRow In DataGridView1.Rows
                If Not IsDBNull(row.Cells(4).Value) Then
                    If row.Cells(4).Value = "1" Then
                        RESPONSE.Items.Add("FOR")
                        RESPONSE.Items.Add("AGAINST")
                        RESPONSE.Items.Add("ABSTAIN")
                        RESPONSE.Items.Add("INVALID")
                        RESPONSE.Items.Add("NULL")
                    End If
                    If row.Cells(4).Value = "2" Then
                        RESPONSE.Items.Add("YES")
                        RESPONSE.Items.Add("NO")
                        RESPONSE.Items.Add("ABSTAIN")
                        RESPONSE.Items.Add("INVALID")
                        RESPONSE.Items.Add("NULL")
                    End If
                End If
            Next

    Tuesday, March 06, 2018 8:12 PM

All replies

  • Hi

    A little confused on question here, but, best guess as follows.

    Assuming one ComboBox on Form called RESPONSE, and wanting to populate with those fixed items depending on value in a cell(here I used column 1 as I already had a DGV for testing).

    Using this code, then when user places selection in a row, then the combobox will be populated accordingly.

      Private Sub DataGridView1_RowEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
    	RESPONSE.Items.Clear()
    	Select Case CInt(DataGridView1("Roll Number", e.RowIndex).Value)
    	  Case 1
    		RESPONSE.Items.Add("FOR")
    		RESPONSE.Items.Add("AGAINST")
    		RESPONSE.Items.Add("ABSTAIN")
    		RESPONSE.Items.Add("INVALID")
    		RESPONSE.Items.Add("NULL")
    	  Case 2
    		RESPONSE.Items.Add("YES")
    		RESPONSE.Items.Add("NO")
    		RESPONSE.Items.Add("ABSTAIN")
    		RESPONSE.Items.Add("INVALID")
    		RESPONSE.Items.Add("NULL")
    	End Select
      End Sub


    Regards Les, Livingston, Scotland

    Tuesday, March 06, 2018 9:06 PM
  • Hi RBRID,

    As I know, a DataGridViewComboBoxCell has no SelectedIndex or SelectedValue property.

    You can set the value directly like this:

    CType(Me.DataGridView1(e.RowIndex, e.ColumnIndex), DataGridViewComboBoxCell).Value = "your value string"

    Or using the index of the items collection: (this works only if you have not set ValueMember and DisplayMember properties)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dt As New DataTable
            dt.Columns.Add("Id", GetType(Int32))
            dt.Columns.Add("Column1", GetType(String))
            dt.Columns.Add("Column2", GetType(String))
            dt.Columns.Add("Column3", GetType(String))
            dt.Columns.Add("Column4", GetType(Int32))
    
            dt.Rows.Add(1, "a", "a", "a", 1)
            dt.Rows.Add(1, "b", "b", "b", 1)
            dt.Rows.Add(1, "c", "c", "c", 2)
            dt.Rows.Add(1, "d", "d", "d", 2)
    
            DataGridView1.DataSource = dt
    
            Dim combox As New DataGridViewComboBoxColumn
            combox.HeaderText = "select items"
            combox.Name = "cmd"
            DataGridView1.Columns.Add(combox)
            fun()
        End Sub
        Dim cmd As DataGridViewComboBoxCell
        Private Sub fun()
            For Each row As DataGridViewRow In DataGridView1.Rows
                cmd = DirectCast(row.Cells(5), DataGridViewComboBoxCell)
                If Not IsDBNull(row.Cells(4).Value) Then
                    If row.Cells(4).Value = "1" Then
                        cmd.Items.Add("FOR")
                        cmd.Items.Add("AGAINST")
                        cmd.Items.Add("ABSTAIN")
                        cmd.Items.Add("INVALID")
                        cmd.Items.Add("NULL")
                        cmd.Value = cmd.Items(4)
                    End If
                    If row.Cells(4).Value = "2" Then
                        cmd.Items.Add("YES")
                        cmd.Items.Add("NO")
                        cmd.Items.Add("ABSTAIN")
                        cmd.Items.Add("INVALID")
                        cmd.Items.Add("NULL")
                        cmd.Value = cmd.Items(4)
                    End If
                End If
            Next
        End Sub
    

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 07, 2018 3:23 AM
    Moderator
  • Thank you!!
    Thursday, March 08, 2018 7:49 PM
  • Thank you!
    Thursday, March 08, 2018 7:50 PM
  • Hi RBRID,

    If your issue has been resolved, please remember to close you thread by click marking the helpful post as answer, it is beneficial to other communities who have the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 09, 2018 1:26 AM
    Moderator