Solved

CharacterCasing DataGridView VB.NET

Posted on 2013-01-03
8
1,936 Views
Last Modified: 2013-01-12
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
Comment
Question by:Thomas Swaney
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 250 total points
ID: 38743827
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
 
LVL 2

Author Comment

by:Thomas Swaney
ID: 38744061
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
 
LVL 2

Author Comment

by:Thomas Swaney
ID: 38744556
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
Industry Leaders: 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!

 
LVL 2

Author Comment

by:Thomas Swaney
ID: 38744730
Points Increase for anyone that has an answer to this using CharacterCasing.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 38745677
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
 
LVL 2

Author Comment

by:Thomas Swaney
ID: 38748039
Can you please show me an example of how to do this as I am new to programming in VB.NET?
0
 
LVL 2

Accepted Solution

by:
Thomas Swaney earned 0 total points
ID: 38752786
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
 
LVL 2

Author Closing Comment

by:Thomas Swaney
ID: 38769734
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an explanation of a simple data model to help parse a JSON feed
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question