Link to home
Start Free TrialLog in
Avatar of ljhodgett
ljhodgett

asked on

select item in combibox contained within a datagridview in vb.net 2005

Hi,

I have the following code: -

Public Class Form1

    Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        If TypeOf e.Control Is ComboBox Then
            Dim cbo As ComboBox = CType(e.Control, ComboBox)
            cbo.Items.Clear()
            Select Case Me.DataGridView1.CurrentRow.Index
                Case 0
                    cbo.Items.Add("1.1")
                    cbo.Items.Add("1.2")
                    cbo.SelectedItem = 1
                Case 1
                    cbo.Items.Add("2.1")
                    cbo.Items.Add("2.2")
                    cbo.Items.Add("2.3")

                Case 2
                    cbo.Items.Add("3.1")

            End Select
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGridView1.Rows.Add("Test")
    End Sub
End Class


What it does is populate a datagridview with a straing column and anothe column with a combibox that is populated individually. When the combibox is populated it does not show the first piece of text i,e, if the combibox has : -

1.1
1.2
1.3

it shows the combibox as being blank until you clikc the down arrow. Usually with standard from combiboxes I use the code: -

cmbtest.selecteditem = 1

but this does not seem to work with combiboxes that are contained within a datagridview.

I would appreciate your help.

Many Thanks
Lee
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
 Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing

    Dim cbo As ComboBox = CType(e.Control, ComboBox)
    cbo.Items.Clear()

    Dim rowIndex As Integer = Me.DataGridView1.CurrentRow.Index

    ' Set the items for the current row.
    For index As Integer = 1 To 5
      Dim text As String = (rowIndex + 1) & "." & index
      cbo.Items.Add(text)
    Next

    ' Highlight the current text from the cell in the ComboBox.
    Dim cell As DataGridViewCell = Me.DataGridView1.CurrentRow.Cells(Me.Column1.Index)

    cbo.SelectedIndex = cbo.FindString(cell.Value)

  End Sub

Bob
Avatar of ljhodgett
ljhodgett

ASKER

Hi Bob,

I've tried the following code: -

Public Class Form1

    Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        If TypeOf e.Control Is ComboBox Then
            Dim cbo As ComboBox = CType(e.Control, ComboBox)
            cbo.Items.Clear()
            Select Case Me.DataGridView1.CurrentRow.Index
                Case 0
                    cbo.Items.Add("1.1")
                    cbo.Items.Add("1.2")
                    'Dim cell As DataGridViewCell = Me.DataGridView1.CurrentRow.Cells(Me.Column1.Index)

                    'cbo.SelectedIndex = cbo.FindString(cell.Value)
                Case 1
                    cbo.Items.Add("2.1")
                    cbo.Items.Add("2.2")
                    cbo.Items.Add("2.3")

                Case 2
                    cbo.Items.Add("3.1")

            End Select
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGridView1.Rows.Add("Test")
    End Sub
End Class

When I click the button it usually runs the edit control procedure but doesn't seem to in this case.

Best Regards
Lee
Lee,

You have to keep something in mind with the DataGridView and combo box columns:

1) The cell value needs to be found in the Items collection

2) The Items collection for the editing ComboBox can be changed in the EditingControlShowing event handler

3) You need to set the SelectedIndex for the editing ComboBox, or it will display a blank in the text portion when you click on the dropdown button.

4) Create a new test form, and try that sample code out, and see how it works.

Bob
Sorry Bob,

Pasted the last code wrong. The code I tried was: -

Public Class Form1

    Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        Dim cbo As ComboBox = CType(e.Control, ComboBox)
        cbo.Items.Clear()

        Dim rowIndex As Integer = Me.DataGridView1.CurrentRow.Index

        ' Set the items for the current row.
        For index As Integer = 1 To 5
            Dim text As String = (rowIndex + 1) & "." & index
            cbo.Items.Add(text)
        Next

        ' Highlight the current text from the cell in the ComboBox.
        Dim cell As DataGridViewCell = Me.DataGridView1.CurrentRow.Cells(Me.Column1.Index)

        cbo.SelectedIndex = cbo.FindString(cell.Value)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGridView1.Rows.Add("Test")
    End Sub
End Class

Best Regards
Lee
I set up a test DataGridView with only 1 combobox column, and used the first block of sample code in the Form_Load event.

Bob
Hi Bob,

Can you please paste the code as I can get an idea as I'm struggling a bit.

Many Thanks
Lee
Lee,
I pasted the code above.  The only thing is that I didn't include the .designer, but it is only a simple DataGridView with 1 combobox column, and AutoGenerateColumns = false.

Bob
Hi Bob,

I've got it now thank you. When you said block one I thought you were referring to the first procedure in the last thread. I realise now you meant the first thread as block. It's working now so I can start finding out how to use the datagridview in it's entirety.

Many Thanks
Lee