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_EditingContr olShowing( ByVal sender As Object, ByVal e As System.Windows.Forms.DataG ridViewEdi tingContro lShowingEv entArgs) Handles DataGridView1.EditingContr olShowing
If TypeOf e.Control Is ComboBox Then
Dim cbo As ComboBox = CType(e.Control, ComboBox)
cbo.Items.Clear()
Select Case Me.DataGridView1.CurrentRo w.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("Te st")
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
I have the following code: -
Public Class Form1
Private Sub DataGridView1_EditingContr
If TypeOf e.Control Is ComboBox Then
Dim cbo As ComboBox = CType(e.Control, ComboBox)
cbo.Items.Clear()
Select Case Me.DataGridView1.CurrentRo
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("Te
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Bob,
I've tried the following code: -
Public Class Form1
Private Sub DataGridView1_EditingContr olShowing( ByVal sender As Object, ByVal e As System.Windows.Forms.DataG ridViewEdi tingContro lShowingEv entArgs) Handles DataGridView1.EditingContr olShowing
If TypeOf e.Control Is ComboBox Then
Dim cbo As ComboBox = CType(e.Control, ComboBox)
cbo.Items.Clear()
Select Case Me.DataGridView1.CurrentRo w.Index
Case 0
cbo.Items.Add("1.1")
cbo.Items.Add("1.2")
'Dim cell As DataGridViewCell = Me.DataGridView1.CurrentRo w.Cells(Me .Column1.I ndex)
'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("Te st")
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
I've tried the following code: -
Public Class Form1
Private Sub DataGridView1_EditingContr
If TypeOf e.Control Is ComboBox Then
Dim cbo As ComboBox = CType(e.Control, ComboBox)
cbo.Items.Clear()
Select Case Me.DataGridView1.CurrentRo
Case 0
cbo.Items.Add("1.1")
cbo.Items.Add("1.2")
'Dim cell As DataGridViewCell = Me.DataGridView1.CurrentRo
'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("Te
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
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
ASKER
Sorry Bob,
Pasted the last code wrong. The code I tried was: -
Public Class Form1
Private Sub DataGridView1_EditingContr olShowing( ByVal sender As Object, ByVal e As System.Windows.Forms.DataG ridViewEdi tingContro lShowingEv entArgs) Handles DataGridView1.EditingContr olShowing
Dim cbo As ComboBox = CType(e.Control, ComboBox)
cbo.Items.Clear()
Dim rowIndex As Integer = Me.DataGridView1.CurrentRo w.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.CurrentRo w.Cells(Me .Column1.I ndex)
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("Te st")
End Sub
End Class
Best Regards
Lee
Pasted the last code wrong. The code I tried was: -
Public Class Form1
Private Sub DataGridView1_EditingContr
Dim cbo As ComboBox = CType(e.Control, ComboBox)
cbo.Items.Clear()
Dim rowIndex As Integer = Me.DataGridView1.CurrentRo
' 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.CurrentRo
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("Te
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
Bob
ASKER
Hi Bob,
Can you please paste the code as I can get an idea as I'm struggling a bit.
Many Thanks
Lee
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
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
ASKER
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
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
Dim cbo As ComboBox = CType(e.Control, ComboBox)
cbo.Items.Clear()
Dim rowIndex As Integer = Me.DataGridView1.CurrentRo
' 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.CurrentRo
cbo.SelectedIndex = cbo.FindString(cell.Value)
End Sub
Bob