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.