-
July 31st, 2008, 05:24 AM
#1
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.
Any ideas?
Cheers
Visual Basic 2005 ver. 8.0.50727.867
-
July 31st, 2008, 05:31 AM
#2
Re: Editing Text in a DataGridViewComboBoxColumn
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.
Visual Basic 2005 ver. 8.0.50727.867
-
July 31st, 2008, 06:16 AM
#3
Re: Editing Text in a DataGridViewComboBoxColumn
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.
*Guarantee may not be honoured.
-
July 31st, 2008, 06:20 AM
#4
Re: Editing Text in a DataGridViewComboBoxColumn
Hiya
Cheers for that - will give it a go and let ya know how I get on!
Visual Basic 2005 ver. 8.0.50727.867
-
July 31st, 2008, 06:35 AM
#5
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?
Visual Basic 2005 ver. 8.0.50727.867
-
July 31st, 2008, 07:50 AM
#6
Re: Editing Text in a DataGridViewComboBoxColumn
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
-
July 31st, 2008, 07:56 AM
#7
Re: Editing Text in a DataGridViewComboBoxColumn
Hi Hannes thanks for the reply. It's quite similar to the code I have in my 1st post above but will give it a go and let you know how I get on!
Visual Basic 2005 ver. 8.0.50727.867
-
July 31st, 2008, 09:15 AM
#8
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
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.
try this now, and see if it also works for you
-
July 31st, 2008, 09:23 AM
#9
Re: Editing Text in a DataGridViewComboBoxColumn
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!
-
July 31st, 2008, 09:33 AM
#10
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.
*Guarantee may not be honoured.
-
July 31st, 2008, 09:34 AM
#11
Re: Editing Text in a DataGridViewComboBoxColumn
LOL - brilliant cheers Hannes will try it out now
Visual Basic 2005 ver. 8.0.50727.867
-
July 31st, 2008, 09:46 AM
#12
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
Oh, I've got Option Strict on, so everything is CTyped
Cool code!
Let me update mine to reflect that as well.
-
July 31st, 2008, 09:47 AM
#13
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?
Cheers
Visual Basic 2005 ver. 8.0.50727.867
-
July 31st, 2008, 09:53 AM
#14
Re: Editing Text in a DataGridViewComboBoxColumn
Voila!
Code:
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.
*Guarantee may not be honoured.
-
July 31st, 2008, 10:44 AM
#15
Re: Editing Text in a DataGridViewComboBoxColumn
That's great - cheers for that. Trying it out now . . . .
Yep - that's great fellas cheers once again for the help
Last edited by brjames32; July 31st, 2008 at 10:51 AM.
Visual Basic 2005 ver. 8.0.50727.867
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|