If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Editing Text in a DataGridViewComboBoxColumn

Hi guys it's me again!

I have a bit of a dilemma - in the ComboBoxColumn, I have added various numbers from 0 to 11. I need people to be able to type into the ComboBoxes in that column if the Combobox doesn't contain a value they need.

I realise that I have to change the ComboBoxStyle to DropDown but am not sure how to. Also, for each row in the ComboBox, I believe I have to add the item that the user typed to the ComboBox list items for it to save but am also unsure how to do it.

All I need now is each time the user changes to the next ComboBox in the column, the text typed into the previous one is saved into that list. Would something like this code I found on the net be along the right lines?:

Code:

Public Sub DataGridView1_CellValidating(ByVal e As DataGridViewCellValidatingEventArgs)
' Adds typed text to the ComboBox items
Dim comboBoxColumn As DataGridViewComboBoxColumn
If (e.ColumnIndex = comboBoxColumn.Index) Then
If (comboBoxColumn.Items.Contains(e.FormattedValue)) Then
comboBoxColumn.Items.Add(e.FormattedValue)
End If
End If
End Sub

You can even save added values, as DataGridViewComboBoxColumn is a valid datatype for the My.Settings object

Last edited by javajawa; July 31st, 2008 at 06:18 AM.

Help from me is always guaranteed!*
VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

Re: Editing Text in a DataGridViewComboBoxColumn

That is definitely more along the lines!

What I really need it to be able to do though is when the user clicks on the combobox cell, they should be able to type into it and that value should be saved instead of having to type into an input box if that is possible?

Re: Editing Text in a DataGridViewComboBoxColumn

I have it!!! Yes! I'm just as excited as you!

This is what you must do, we both were on the right track!
Do this :

Code:

Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
If coladd Then
Dim comboBoxColumn As DataGridViewComboBoxColumn = DataGridView1.Columns(6)
If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
comboBoxColumn.Items.Add(e.FormattedValue)
End If
End If
End If
End Sub
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Dim comboBoxColumn As DataGridViewComboBoxColumn = DataGridView1.Columns(6)
If (DataGridView1.CurrentCellAddress.X = comboBoxColumn.DisplayIndex) Then
Dim cb As ComboBox = e.Control
If (cb IsNot Nothing) Then
cb.DropDownStyle = ComboBoxStyle.DropDown
End If
End If
End Sub

Re: Editing Text in a DataGridViewComboBoxColumn

I've just tried you wonderful code - it's brilliant, apart from (on this machine, at least) the value is cleared. So I changed the code to:

Code:

Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
If ColAdd Then
Dim comboBoxColumn As DataGridViewComboBoxColumn = CType(DataGridView1.Columns(6), DataGridViewComboBoxColumn)
If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
comboBoxColumn.Items.Add(e.FormattedValue)
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = comboBoxColumn.Items(comboBoxColumn.Items.Count - 1)
End If
End If
End If
End Sub

Oh, I've got Option Strict on, so everything is CTyped

Help from me is always guaranteed!*
VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

Re: Editing Text in a DataGridViewComboBoxColumn

Originally Posted by javajawa

I've just tried you wonderful code - it's brilliant, apart from (on this machine, at least) the value is cleared. So I changed the code to:

Code:

Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
If ColAdd Then
Dim comboBoxColumn As DataGridViewComboBoxColumn = CType(DataGridView1.Columns(6), DataGridViewComboBoxColumn)
If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
comboBoxColumn.Items.Add(e.FormattedValue)
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = comboBoxColumn.Items(comboBoxColumn.Items.Count - 1)
End If
End If
End If
End Sub

Re: Editing Text in a DataGridViewComboBoxColumn

That code works great cheers for the help! One last thing - the penalty column is fine so changed the column number to 10. I also need to do the same for column 11 so how can i get the code working for both columns?

Re: Editing Text in a DataGridViewComboBoxColumn

Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
If ColAdd Then
If (e.ColumnIndex = DataGridView1.Columns(10).DisplayIndex) Then
Dim comboBoxColumn As DataGridViewComboBoxColumn = CType(DataGridView1.Columns(10), DataGridViewComboBoxColumn)
If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
comboBoxColumn.Items.Add(e.FormattedValue)
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = comboBoxColumn.Items(comboBoxColumn.Items.Count - 1)
End If
ElseIf (e.ColumnIndex = DataGridView1.Columns(11).DisplayIndex) Then
Dim comboBoxColumn As DataGridViewComboBoxColumn = CType(DataGridView1.Columns(11), DataGridViewComboBoxColumn)
If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
comboBoxColumn.Items.Add(e.FormattedValue)
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = comboBoxColumn.Items(comboBoxColumn.Items.Count - 1)
End If
End If
End If
End Sub
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If (DataGridView1.CurrentCellAddress.X = DataGridView1.Columns(10).DisplayIndex) Then
Dim cb As ComboBox = CType(e.Control, ComboBox)
If (cb IsNot Nothing) Then
cb.DropDownStyle = ComboBoxStyle.DropDown
End If
ElseIf (DataGridView1.CurrentCellAddress.X = DataGridView1.Columns(11).DisplayIndex) Then
Dim cb As ComboBox = CType(e.Control, ComboBox)
If (cb IsNot Nothing) Then
cb.DropDownStyle = ComboBoxStyle.DropDown
End If
End If
End Sub

This is set for columns 10 and 11, ad can be extended to any number of columns by copying the code as I hope should be obvious

Last edited by javajawa; July 31st, 2008 at 09:58 AM.

Help from me is always guaranteed!*
VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.