Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2153
  • Last Modified:

CharacterCasing DataGridView VB.NET

I hope that someone can help with this issue.

I have a simple grid with three columns. I want only Column 2 (index 1) to be uppercase.

In the EditingControlShowing Event I have the following code which effectively sets the text when typing to uppercase although when clicking in Column 1 (index 0) or Column 3 (index 2) the text is capitalized before starting editing. In these columns the text that is entered when typing is lowercase once you have entered the cell although the text that was capitalized on initial entry is still capitalized. Any suggestions would be greatly appreciated.

If TypeOf e.Control Is TextBox And UlfDataGridView.Columns(UlfDataGridView.CurrentCell.ColumnIndex).Name = "DbloginnameDataGridViewTextBoxColumn" Then
      DirectCast(e.Control, TextBox).CharacterCasing = CharacterCasing.Upper
Else
      DirectCast(e.Control, TextBox).CharacterCasing = CharacterCasing.Normal
End If
0
Thomas Swaney
Asked:
Thomas Swaney
  • 6
  • 2
2 Solutions
 
Bob LearnedCommented:
This sounds like a problem with the TextBox control that is used for the editor, since I believe that it is shared for all the cells.

Another approach might be to attach an event handler to the KeyDown event, and convert the characters to upper case only when typing in the cell.
0
 
Thomas SwaneyOwnerAuthor Commented:
I realize that I could use the KeyDown event to do this but I was hoping to find an answer for doing this with charactercasing. I would assume there has to be a way.

Is there a way to set the TextBox control's charactercasing back to normal when the cell gets focus or leaving the cell (cellendedit)? I can't seem to figure out how this would be done. I am new to VB.NET programming.

The problem that I am having there is that I can't seem to reference the textbox. In other words if I tried to use the following code it will not work because e is set to  ByVal e As System.Windows.Forms.DataGridViewCellEventArgs instead of ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs and I don't know how to find the control.

DirectCast(e.Control, TextBox).CharacterCasing = CharacterCasing.Normal
0
 
Thomas SwaneyOwnerAuthor Commented:
Note: When I tab to any column the casing works as it should but if I click into the cell is when I have the problem shown above.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Thomas SwaneyOwnerAuthor Commented:
Points Increase for anyone that has an answer to this using CharacterCasing.
0
 
Bob LearnedCommented:
You don't really have a reference outside of the EditorControlShowing, so you would need create a module-level reference to the TextBox control that is used with that column.  Then, you might be able to change CharacterCasing in the Enter event handler.
0
 
Thomas SwaneyOwnerAuthor Commented:
Can you please show me an example of how to do this as I am new to programming in VB.NET?
0
 
Thomas SwaneyOwnerAuthor Commented:
This is the code that I finally have went with. Apparently there isn't an easy way to set the CharacterCasing back to lower case before re-entering or leaving the text box.

Private Sub DataGridView1_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles UlfDataGridView.CellValidated
 
If (e.RowIndex >= 0) And (e.ColumnIndex >= 1) AndAlso (TypeOf DirectCast(sender, DataGridView).Item(e.ColumnIndex, e.RowIndex).Value Is String) Then

  DirectCast(sender, DataGridView).Item(e.ColumnIndex, e.RowIndex).Value = CType(DirectCast(sender, DataGridView).Item(e.ColumnIndex, e.RowIndex).Value, String).ToUpper
 End If
End Sub

Open in new window

0
 
Thomas SwaneyOwnerAuthor Commented:
My ideal approach would have been to be able to set the CharacterCasing back to lower. From comments here, reading and research it appears that this is not possible until after the user enters the control. In this case the controls/columns that I would not want in uppercase would be converted to uppercase before the user entered the text box they were editing.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now