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.
Right, I now have the ComboBoxStyle changed to DropDown which allows the user to type into the combobox using the following:
Code:
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If DataGridView1.CurrentCellAddress.X = Description.Index Then
Dim cbo As ComboBox = CType(e.Control, ComboBox)
cbo.DropDownStyle = ComboBoxStyle.DropDown
End If
End Sub
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
It doesn't seem to do anything though!
Anyone?
Last edited by brjames32; July 31st, 2008 at 05:59 AM.
Hello Again!
My solution to this would go something like this (after adding a "New" option to the combobox; also, I'm not using the proper table, so the column index and things may need changing)
Code:
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
If e.ColumnIndex = 3 Then
If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString() = "New" Then
Dim ComboCol As DataGridViewComboBoxColumn = CType(DataGridView1.Columns(3), DataGridViewComboBoxColumn)
ComboCol.Items.Insert(ComboCol.Items.Count - 1, InputBox("New Item"))
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = ComboCol.Items(ComboCol.Items.Count - 2)
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.
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?
Perhaps if you could use the DataGridView's EditingControlShowing event ¿ something like this :
Code:
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 cb As ComboBox = TryCast(e.Control, ComboBox)
cb.DropDownStyle = ComboBoxStyle.DropDown
cb.Items.Insert(cb.Items.Count - 1, cb.Text)
End If
End Sub
This allows you to type a value into that particular cell. It may not be 100% complete, but will put you on the right track
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
You must just please add a variable called coladd at the top, like :
Code:
Dim coladd As Boolean
Else, it will throw an error in your CellValidating event.
Ah, we need to set the coladd to true after adding the PenaltyColumn, like this :
Code:
Dim PenaltyColumn As New DataGridViewComboBoxColumn()
With PenaltyColumn
.DataPropertyName = "Penalty"
.HeaderText = "Penalty"
.DropDownWidth = 160
.Width = 90
.MaxDropDownItems = 3
.FlatStyle = FlatStyle.Flat
.Items.Add("1")
.Items.Add("11")
End With
DataGridView1.Columns.Add(PenaltyColumn)
coladd = True
This prevents the conversion error. The Cellvalidating event does fire at the form's startup as well, so this ensures we have the penalty column - then test.
Sorry, I forgot to mention it in my previous post, I got too excited!
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.
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
Cool code!
Let me update mine to reflect that as well.
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?
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.
Bookmarks