Click to See Complete Forum and Search --> : Setting .maxlength for a DataGridViewComboBoxColumn
brjames32
October 22nd, 2008, 06:28 AM
Hi
I have a DataGridView with 2 ComboBoxColumns in it - Description and Action.
I need to be able to limit the amount of characters that users can type into the ComboBoxes within these columns but am not sure how to do it. I have tried:
With Description
.maxlength = 20
.Name = "Description"
.HeaderText = "Description"
.Width = 275
.Visible = True
.ReadOnly = False
'.AutoComplete = True
.Items.Add("2nd Homework deadline missed")
End With
DataGridView1.Columns.Add(Description)
coladd = True
But .maxlength doesn't seem to apply to ComboBoxColumns!
Anyone got any ideas?
Thanks
javajawa
October 22nd, 2008, 07:36 AM
why not just test add a quick test of length in the keypress event? All you have to do is stop the character being processed if there's 20 already there.
HanneSThEGreaT
October 22nd, 2008, 07:47 AM
Another way is to do the validation after the text has been entered. For example, we could use the DataGridView1_CellValidating event, do a quick test; If the text entered is less than 21 characters, fine, add it to the list, everything is hunky dory; if not, inform the user and let him / her enter the text again..
As in :
Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
Dim NumChars As Integer
If ColAdd Then
'Create a New ComboBoxColumn Object, And Cast The dataGridView's Column To That
Dim comboBoxColumn As DataGridViewComboBoxColumn = CType(DataGridView1.Columns(5), DataGridViewComboBoxColumn)
'If In ComboBoxColumn
If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
NumChars = Len(e.FormattedValue)
If NumChars < 21 Then
'Add The Text Entered By The User
comboBoxColumn.Items.Add(e.FormattedValue)
' Make Sure Value Stays Displayed ( May HAve To Enter Value Twice )
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = comboBoxColumn.Items(comboBoxColumn.Items.Count - 1)
Else
MessageBox.Show("Text Too Long")
End If
End If
End If
End If
End Sub
I hope this info was useful.
javajawa
October 22nd, 2008, 07:51 AM
Of course, if you're using the extended DGV which HanneS and I put together, you could add it into the validation:
Private Sub DGV_Validate(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs) Handles MyBase.CellValidating
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
If Len(e.FormattedValue) > 20 Then
Throw New ArgumentException("The Value is too long")
Else
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
brjames32
October 22nd, 2008, 08:14 AM
Cheers guys - glad you remembered my code!
Will try out your suggestions and let you know how I get on!
brjames32
October 22nd, 2008, 08:55 AM
Hi Guys - no joy I'm afraid
The code I am using is below:
Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
Dim Numchars As Integer 'new
If coladd Then
'Create a New ComboBoxColumn Object, And Cast The dataGridView's Column To That
Dim comboBoxColumn As DataGridViewComboBoxColumn = CType(DataGridView1.Columns(9), DataGridViewComboBoxColumn)
'If In ComboBoxColumn
If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
Numchars = Len(e.FormattedValue)
If Numchars < 21 Then
'Add The Text Entered By The User
comboBoxColumn.Items.Add(e.FormattedValue)
' Make Sure Value Stays Displayed ( May HAve To Enter Value Twice )
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = comboBoxColumn.Items(comboBoxColumn.Items.Count - 1)
Else
MessageBox.Show("Description Text Too Long")
End If
End If
End If
Else
Dim comboBoxColumn As DataGridViewComboBoxColumn = CType(DataGridView1.Columns(10), DataGridViewComboBoxColumn)
'If In ComboBoxColumn
If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
Numchars = Len(e.FormattedValue)
If Numchars < 21 Then
'Add The Text Entered By The User
comboBoxColumn.Items.Add(e.FormattedValue)
' Make Sure Value Stays Displayed ( May HAve To Enter Value Twice )
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = comboBoxColumn.Items(comboBoxColumn.Items.Count - 1)
Else
MessageBox.Show("Action Text Too Long")
End If
End If
End If
End If
End Sub
Thanks for the help so far . . . . .
javajawa
October 22nd, 2008, 09:02 AM
What error are you getting, and when? This code segment compiles fine on my machine, but it may not work in context.
Plus, if I remember correctly, if is colAdd is true then the input is not generated by the user, so you don't need to check it? Or am I thinking of a colAdd from a different project?
javajawa
October 22nd, 2008, 09:09 AM
also, a slightly neaten version of the code you posted (Mainly perfectionist things).
The only important change is the inclusion of e.Cancel = True after the messageboxs.
Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
If coladd Then
'Create a New ComboBoxColumn Object, And Cast The dataGridView's Column To That
Dim comboBoxColumn As DataGridViewComboBoxColumn = CType(DataGridView1.Columns(9), DataGridViewComboBoxColumn)
'If In ComboBoxColumn
If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
If Len(e.FormattedValue) < 21 Then
'Add The Text Entered By The User
comboBoxColumn.Items.Add(e.FormattedValue)
' Make Sure Value Stays Displayed (May Have To Enter Value Twice)
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = comboBoxColumn.Items(comboBoxColumn.Items.Count - 1)
Else
MessageBox.Show("Description Text Too Long")
e.Cancel = True
End If
End If
End If
Else
Dim comboBoxColumn As DataGridViewComboBoxColumn = CType(DataGridView1.Columns(10), DataGridViewComboBoxColumn)
'If In ComboBoxColumn
If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
If Len(e.FormattedValue) < 21 Then
'Add The Text Entered By The User
comboBoxColumn.Items.Add(e.FormattedValue)
'Make Sure Value Stays Displayed (May Have To Enter Value Twice)
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = comboBoxColumn.Items(comboBoxColumn.Items.Count - 1)
Else
MessageBox.Show("Action Text Too Long")
e.Cancel = True
End If
End If
End If
End If
End Sub
brjames32
October 22nd, 2008, 09:12 AM
I think you are thinking of a CollAdd from a different project!
The code does not produce an error - it just adds the entry to the ComboBox anyway! It just seems to work as normal as if I hadn't added the new code.
javajawa
October 22nd, 2008, 09:13 AM
Look at e.Cancel (see my previous post (post 8))
EDIT: clearly I'm not thinking, because even if that isn't there, the item should never be added. It must be something else. Have you tried stepping through it and checking the value and length of e.FormattedValue?
brjames32
October 22nd, 2008, 09:39 AM
Yep - that's true. Will step though now and see what is happening ;)
brjames32
October 22nd, 2008, 09:55 AM
Doh - I am being thick. I haven't stepped through any code for a loooonnnnnggg time and now I can't remember how to isolate the piece of code (e.FormattedValue) that I need the value of!!
Can you give me some pointers??
Cheers
javajawa
October 22nd, 2008, 09:59 AM
I personally use the Watch windows (Menu Bar -> Debug -> Windows -> Watches) as then, when debugging, you can specify expressions to watch. Other methods such as just selecting an expression and mousing over, or the Local or Auto windows (same menu) also do the job.
brjames32
October 23rd, 2008, 06:05 AM
Have tried but setting a breakpoint on the following line but nothing happens so I guess it somehow isn't executing that line?
comboBoxColumn.Items.Add(e.FormattedValue)
The code just executes as normal and doesn't stop at the line. The item does get added to the ComboBox though and when I subsequently save, it gets saved to the Spreadsheet too!
Also, when I goto Debug --> Windows, all I get is Immediate, Breakpoints and Outputs. I don't have Watch
brjames32
October 23rd, 2008, 09:50 AM
Anyone?????
HanneSThEGreaT
October 23rd, 2008, 10:14 AM
Hi again! :)
Can you perhaps show the complete code segment ¿
Is it still the same app I'm thinking about ¿
There may be an outside influence causing this, but the code I supplied worked as you needed it, javajawa's as well.
This is indeed an odd one.. :)
brjames32
October 24th, 2008, 02:18 AM
Hi Hannes thanks for the reply
It is indeed the same app you are thinking about. The full code segment for the Cell Validating section is:
Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
Dim Numchars As Integer 'new
If coladd Then
'Create a New ComboBoxColumn Object, And Cast The dataGridView's Column To That
Dim comboBoxColumn As DataGridViewComboBoxColumn = CType(DataGridView1.Columns(9), DataGridViewComboBoxColumn)
'If In ComboBoxColumn
If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
Numchars = Len(e.FormattedValue)
If Numchars < 21 Then
'Add The Text Entered By The User
comboBoxColumn.Items.Add(e.FormattedValue)
' Make Sure Value Stays Displayed ( May HAve To Enter Value Twice )
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = comboBoxColumn.Items(comboBoxColumn.Items.Count - 1)
Else
MessageBox.Show("Description Text Too Long")
e.Cancel = True
End If
End If
End If
Else
Dim comboBoxColumn As DataGridViewComboBoxColumn = CType(DataGridView1.Columns(10), DataGridViewComboBoxColumn)
'If In ComboBoxColumn
If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
Numchars = Len(e.FormattedValue)
If Numchars < 21 Then
'Add The Text Entered By The User
comboBoxColumn.Items.Add(e.FormattedValue)
' Make Sure Value Stays Displayed ( May HAve To Enter Value Twice )
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = comboBoxColumn.Items(comboBoxColumn.Items.Count - 1)
Else
MessageBox.Show("Action Text Too Long")
e.Cancel = True
End If
End If
End If
End If
End Sub
Thanks for taking a look Hannes
HanneSThEGreaT
October 24th, 2008, 02:52 AM
OK, Change your CellValidating event to look like this code, precisely :
Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
Dim NumChars As Integer
If TypeOf (DataGridView1.Columns(e.ColumnIndex)) Is DataGridViewComboBoxColumn Then
Dim comboBoxColumn As DataGridViewComboBoxColumn = CType(DataGridView1.Columns(e.ColumnIndex), DataGridViewComboBoxColumn)
If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
NumChars = Len(e.FormattedValue)
If NumChars > 20 Then
MessageBox.Show("The Value is too long")
Else
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
It is mainly javajawa's code, but both options supplied would've worked.
Make sure it reflects this precisely, run it, test it, see if it works, and let us know :)
javajawa
October 24th, 2008, 03:49 AM
I'd still personally save declaring the variable, especially if it's only used once!
Also, the code that I've been posting has been working on the assumption that you want the conditions for all DGVComboCols. Hannes' code is of course specific to the two columns.
Just a thought - you're not using both the DGVNew class and that code - because that's the only thing I can think of that would allow the value to be added without the breakpoint being fired, as it would be added by the DGVNew code and then not added again as the combobox would then contain the item...
I really hope I'm making some sense here (It still feels a bit on the early side, even though I don't have enough time to get to my first lecture...
brjames32
October 24th, 2008, 05:22 AM
HI Hannes
That works great - not a clue why it didn't work yesterday. Even me with my limited skills could see that it should have worked!!
Now I just have to stop the value entered from disappearing when the message pops up so that they don't have to type it in again!
Thanks as always for your help guys ;)
javajawa
October 24th, 2008, 08:47 AM
For stopping it disappearing, I'd look into setting the e.Cancel property. I think tha if you set it to true, the underlying validation event won't be called.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.