|
-
October 22nd, 2008, 06:28 AM
#1
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
Visual Basic 2005 ver. 8.0.50727.867
-
October 22nd, 2008, 07:36 AM
#2
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.
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.
-
October 22nd, 2008, 07:47 AM
#3
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.
-
October 22nd, 2008, 07:51 AM
#4
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
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.
-
October 22nd, 2008, 08:14 AM
#5
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!
Visual Basic 2005 ver. 8.0.50727.867
-
October 22nd, 2008, 08:55 AM
#6
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 . . . . .
Visual Basic 2005 ver. 8.0.50727.867
-
October 22nd, 2008, 09:02 AM
#7
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?
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.
-
October 22nd, 2008, 09:09 AM
#8
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
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.
-
October 22nd, 2008, 09:12 AM
#9
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.
Visual Basic 2005 ver. 8.0.50727.867
-
October 22nd, 2008, 09:13 AM
#10
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?
Last edited by javajawa; October 22nd, 2008 at 09:30 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.
-
October 22nd, 2008, 09:39 AM
#11
Re: Setting .maxlength for a DataGridViewComboBoxColumn
Yep - that's true. Will step though now and see what is happening
Visual Basic 2005 ver. 8.0.50727.867
-
October 22nd, 2008, 09:55 AM
#12
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
Visual Basic 2005 ver. 8.0.50727.867
-
October 22nd, 2008, 09:59 AM
#13
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.
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.
-
October 23rd, 2008, 06:05 AM
#14
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
Last edited by brjames32; October 23rd, 2008 at 06:21 AM.
Visual Basic 2005 ver. 8.0.50727.867
-
October 23rd, 2008, 09:50 AM
#15
Re: Setting .maxlength for a DataGridViewComboBoxColumn
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
|