Link to home
Start Free TrialLog in
Avatar of rwheeler23
rwheeler23Flag for United States of America

asked on

What is the proper way to force uppercase on a column in a datagridview?

I have a C# program with a datagridview with 12 columns.  On column [2] I want to force uppercase so the data will not only always display as uppercase but will be written in uppercase.

                dgvViewJobLinker.Columns[2].Width = 100;
                dgvViewJobLinker.Columns[2].ReadOnly = false;
                dgvViewJobLinker.Columns[2].HeaderText = "Transfer to Job";

What is the proper way to force uppercase on a column in a datagridview?
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

I want to force uppercase so the data will not only always display as uppercase but will be written in uppercase.


if you mean to update the data to upper case before the display? I think this already answered your question above?

Avatar of rwheeler23

ASKER

If a user type 2136d8 i want it to immediately display 2136D8. I was able to get it to display uppercase but only after the user exited the field.
I was able to get it to display uppercase but only after the user exited the field.

what's the application platform that you're using? Web Form, Windows Application, etc? you probably need to hook into the onchange or keydown/keypress event to change the text to uppercase.
This is a winform. I think keypress may be the way to go.
My bad, it's no longer that difficult to set it up, so we just need to update the CharacterCasing property to Upper

User generated image
The problem I have is i am dealing with a column in a datagridview not a textbox. I wish this was a property of a datagridview.
in that case, probably the easiest way will be format the data at backend (SQL), so from front end (datagridview) just display the data.
The problem there is I use _commandbulder to write the data so I do write the data as a group. The column in question is blank to start and then the users can start filling in the  blanks. I will play around with some change event or keypress to force uppercase as the user types.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, this was helpful for the display on the screen  however what is written to the database are the lowercase values.  I have a Save button at the bottom of this dgv. I am going to put a loop under this save button to uppercase all values in column [1].  This what I got to work.

        private void dgvViewJobLinker_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            // Check the value of the e.ColumnIndex property if you want to apply this formatting only so some columns.
            if (e.ColumnIndex == 1)
            {
                e.Value = e.Value.ToString().ToUpper();
                e.FormattingApplied = true;
            }
        }
Thanks for these tips. They got me pointed in the right direction.