Solved

CharacterCasing DataGridView VB.NET

Posted on 2013-01-03
8
2,001 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
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

632 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