Solved

CharacterCasing DataGridView VB.NET

Posted on 2013-01-03
8
1,778 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

744 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

13 Experts available now in Live!

Get 1:1 Help Now