Dan Z
Dan Z

Reputation: 101

Convert characters to Upper Case in DataGridView

My form contains several DataGridView controls that have two columns. I want the data that the user types into column 1 to be converted to upper case. The data in column 0 is read only and is populated by my program. It is numeric and does not need to be converted to upper case. The code below works but I'm wondering if there's a better way.

private: System::Void dataGridView_patterns_EditingControlShowing(System::Object^  sender, System::Windows::Forms::DataGridViewEditingControlShowingEventArgs^  e)
    {
        // This event sets the character casing to upper for the patterns.  It is called once per pattern.

        TextBox^ myControl;

        myControl = (TextBox^)(e->Control);
        myControl->CharacterCasing = CharacterCasing::Upper;
    }

The only problem that I have with this code is that the EditingControlShowing event is called once for every row in the DataGridView. Is there a way to set the CharacterCasing to Upper one time for the control, or does it have to be set for every row to work properly? I don't notice any performance issues, but it just seems unnecessary to set the casing for every row in the control.

Thank you!

Upvotes: 1

Views: 1726

Answers (2)

Dan Z
Dan Z

Reputation: 101

Just to close out this thread, I used the code in my original question. I guess that's the easiest way to set the casing in a DataGridView control.

Upvotes: 1

jaredbaszler
jaredbaszler

Reputation: 4839

You can create a custom DataGridTextBoxColumn shown here:

How to set DataGridView columns text format to uppercase by adding new property?

Upvotes: 1

Related Questions