-
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 ;)
-
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?
-
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 :D
-
Re: Editing Text in a DataGridViewComboBoxColumn
Hiya
Cheers for that - will give it a go and let ya know how I get on! :D
-
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
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 :)
-
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!
:)
-
Re: Editing Text in a DataGridViewComboBoxColumn
I have it!!! :D 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 :)
-
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! :D
-
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 :D
-
Re: Editing Text in a DataGridViewComboBoxColumn
LOL - brilliant cheers Hannes will try it out now ;) ;)
-
Re: Editing Text in a DataGridViewComboBoxColumn
Quote:
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 :D
Cool code! :thumb:
Let me update mine to reflect that as well. :)
-
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 :D
-
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 :D
-
Re: Editing Text in a DataGridViewComboBoxColumn
That's great - cheers for that. Trying it out now . . . . :D
Yep - that's great fellas cheers once again for the help ;)
-
Re: Editing Text in a DataGridViewComboBoxColumn
That is good news! Great work guys! :thumb: :thumb:
This was real fun for me! :)
-
Re: Editing Text in a DataGridViewComboBoxColumn
-
Re: Editing Text in a DataGridViewComboBoxColumn
Glad it worked (I love it when something you program blind works, but I couldn't be bothered to switch between solutions :)
You know, it wouldn't be far from the code we've got here to an extended DataGridView control where this was the default behaviour from all columns...
Well, I've got nothing else to do this evening...
-
Re: Editing Text in a DataGridViewComboBoxColumn
LOL - good luck!!! :p
right got wife's brother's wedding tomorrow so going for drinks with the Groom for his last night of freedom ;)
Have a good wekend everyone :thumb: :D
-
Re: Editing Text in a DataGridViewComboBoxColumn
I had a go - the coding worked out simple. It compiles, but it crashes when you try and run a form with one on. Works fine on the designer, and it generates no debug data, so I'm stumped...
Code:
Public Class DataGridView2
Inherits Windows.Forms.DataGridView
Private Debounce As Boolean
Private Sub mCellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles MyBase.CellValidating
If Debounce Then
Debounce = False
Else
If TypeOf (Me.Columns(e.ColumnIndex)) Is DataGridViewComboBoxColumn Then
Dim comboBoxColumn As DataGridViewComboBoxColumn = CType(Me.Columns(e.ColumnIndex), DataGridViewComboBoxColumn)
If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
comboBoxColumn.Items.Add(e.FormattedValue)
Me.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = comboBoxColumn.Items(comboBoxColumn.Items.Count - 1)
End If
End If
End If
End Sub
Private Sub mColumnAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs) Handles MyBase.ColumnAdded
Debounce = True
End Sub
Private Sub DataGridView2_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles MyBase.EditingControlShowing
If TypeOf (MyBase.Columns(MyBase.CurrentCell.ColumnIndex)) Is DataGridViewComboBoxColumn Then
Dim cb As ComboBox = CType(e.Control, ComboBox)
If (cb IsNot Nothing) Then
cb.DropDownStyle = ComboBoxStyle.DropDown
End If
End If
End Sub
End Class
-
Re: Editing Text in a DataGridViewComboBoxColumn
Good morning!
This is what I did, javajawa :
I created a Class Library called DGVNew, the code looks like :
Code:
Imports System.Windows.Forms.DataGridView
Public Class DGVNew
Inherits System.Windows.Forms.DataGridView
Private Debounce As Boolean
Private Sub DGVNew(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles MyBase.CellValidating
If Debounce Then
Debounce = False
Else
If TypeOf (Me.Columns(e.ColumnIndex)) Is System.Windows.Forms.DataGridViewComboBoxColumn Then
Dim comboBoxColumn As System.Windows.Forms.DataGridViewComboBoxColumn = CType(Me.Columns(e.ColumnIndex), System.Windows.Forms.DataGridViewComboBoxColumn)
If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
comboBoxColumn.Items.Add(e.FormattedValue)
Me.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = comboBoxColumn.Items(comboBoxColumn.Items.Count - 1)
End If
End If
End If
End Sub
Private Sub DGVNew(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs) Handles MyBase.ColumnAdded
Debounce = True
End Sub
Private Sub DGVNew_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles MyBase.EditingControlShowing
If TypeOf (MyBase.Columns(MyBase.CurrentCell.ColumnIndex)) Is System.Windows.Forms.DataGridViewComboBoxColumn Then
Dim cb As System.Windows.Forms.ComboBox = CType(e.Control, System.Windows.Forms.ComboBox)
If (cb IsNot Nothing) Then
cb.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown
End If
End If
End Sub
End Class
Just had to reference System.Windows.Forms etc.
I Built DGVNew, in order to get the DLL.
Then, I created a separate project called DGVReplacement. I added the DGVNew.dll to the Toolbox, and added the following code :
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
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
DgvNew1.Columns.Add(PenaltyColumn)
End Sub
End Class
It works perfectly, I've been testing it for some time now, and I haven't had any problems with it :)
Should I include them in a zip ( note it's in 2005, as I have 2008 only at home )
-
Re: Editing Text in a DataGridViewComboBoxColumn
Wouldn't mind the zip. My glitch probably came from the fact that I didn't bother with new projects, but just tore into the designer of the one I'd be using for this before. I probably made some mistake in there...
-
Re: Editing Text in a DataGridViewComboBoxColumn
Here is the zip :)
I hope it helps!
-
Re: Editing Text in a DataGridViewComboBoxColumn
Found the issue - when I changed the designers, I forgot that it CTypes everything to call BeginInit(), so it was CTypeing to the old version. Plus Debugger was told not to show me that bit (there should be some options to turn that off by default. You can get so much fine tuning out of the designer...)
-
Re: Editing Text in a DataGridViewComboBoxColumn
Glad you got it solved!! :thumb:
-
1 Attachment(s)
Re: Editing Text in a DataGridViewComboBoxColumn
Done some work on it - it now validates when you click elsewhere, or if you press enter. I can't work out why I need to do them sperately, but it works (surprisingly enough).
I love pointless class design...
-
Re: Editing Text in a DataGridViewComboBoxColumn
Any objections on me setting up an FAQ covering DataGridViewComboboxColumns ( all these things we recenty did ) ¿
-
Re: Editing Text in a DataGridViewComboBoxColumn
Or even on some of the fun things you can do with DGVs generally? LOL
-
Re: Editing Text in a DataGridViewComboBoxColumn
Hi HannesTheGreat
Sounds good to me!! ;)
-
Editing Text in a DataGridViewComboBoxColumn
hi all.
i hav added a datagricview into winform. i hav added 2 combobx column into that datagrid view.
when i click the firsr combox column the second combobox column value should changed
this is m y reqiurment was. my first combox column is Itemid and second combobx column is Itemdescription.
as my reqirment when i chaged the itemid column the second column itemdescription should be changed.
some one pls help mw for this.
thnxs.
Rishad...