Setting .maxlength for a DataGridViewComboBoxColumn
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:
Code:
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
Re: Setting .maxlength for a DataGridViewComboBoxColumn
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.
Re: Setting .maxlength for a DataGridViewComboBoxColumn
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 :
Code:
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.
Re: Setting .maxlength for a DataGridViewComboBoxColumn
Of course, if you're using the extended DGV which HanneS and I put together, you could add it into the validation:
Code:
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
Re: Setting .maxlength for a DataGridViewComboBoxColumn
Cheers guys - glad you remembered my code!
Will try out your suggestions and let you know how I get on!
Re: Setting .maxlength for a DataGridViewComboBoxColumn
Hi Guys - no joy I'm afraid
The code I am using is below:
Code:
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 . . . . .
Re: Setting .maxlength for a DataGridViewComboBoxColumn
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?
Re: Setting .maxlength for a DataGridViewComboBoxColumn
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.
Code:
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
Re: Setting .maxlength for a DataGridViewComboBoxColumn
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.
Re: Setting .maxlength for a DataGridViewComboBoxColumn
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?
Re: Setting .maxlength for a DataGridViewComboBoxColumn
Yep - that's true. Will step though now and see what is happening ;)
Re: Setting .maxlength for a DataGridViewComboBoxColumn
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
Re: Setting .maxlength for a DataGridViewComboBoxColumn
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.
Re: Setting .maxlength for a DataGridViewComboBoxColumn
Have tried but setting a breakpoint on the following line but nothing happens so I guess it somehow isn't executing that line?
Code:
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
Re: Setting .maxlength for a DataGridViewComboBoxColumn
Re: Setting .maxlength for a DataGridViewComboBoxColumn
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.. :)
Re: Setting .maxlength for a DataGridViewComboBoxColumn
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:
Code:
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
Re: Setting .maxlength for a DataGridViewComboBoxColumn
OK, Change your CellValidating event to look like this code, precisely :
Code:
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 :)
Re: Setting .maxlength for a DataGridViewComboBoxColumn
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...
Re: Setting .maxlength for a DataGridViewComboBoxColumn
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 ;)
Re: Setting .maxlength for a DataGridViewComboBoxColumn
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.