Solved

CharacterCasing DataGridView VB.NET

Posted on 2013-01-03
8
1,820 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
  • 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
 
LVL 2

Author Comment

by:Thomas Swaney
ID: 38744730
Points Increase for anyone that has an answer to this using CharacterCasing.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
A short article about problems I had with the new location API and permissions in Marshmallow
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now