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

28,264

Solution 1

I'm afraid there is no standard property for formatting text how you want.

If you really don't want to use the various DGV events to do your text formatting, you can always create your own DGV components that do what you want and use those in place of the standard DGV components. This article on MSDN should get you started.

EDIT

Here's a blog entry from someone calling himself HanSolo that does what you need.

Here's the code:

public class DataGridViewUpperCaseTextBoxColumn : DataGridViewTextBoxColumn { 
    public DataGridViewUpperCaseTextBoxColumn() : base() { 
        CellTemplate = new DataGridViewUpperCaseTextBoxCell(); 
    } 
}

public class DataGridViewUpperCaseTextBoxCell : DataGridViewTextBoxCell { 
    public DataGridViewUpperCaseTextBoxCell() : base() { } 
    public override Type EditType { 
        get { 
            return typeof(DataGridViewUpperCaseTextBoxEditingControl); 
        } 
    } 
}

public class DataGridViewUpperCaseTextBoxEditingControl : DataGridViewTextBoxEditingControl { 
    public DataGridViewUpperCaseTextBoxEditingControl() : base() { 
        this.CharacterCasing = CharacterCasing.Upper; 
    } 
}

Include this code in your project. Once you do so you'll be able to add a new DataGridViewColumn to your DataGridView of type DataGridViewUpperCaseTextBoxColumn. This new DataGridViewColumn uppercases all text entered in the column's TextBox component.

You should also reconsider your decision to not use events. It's pretty easy to do. For example if you have a DGV named dataGridView1 you can use the CellFormatting event like this:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
     // Check the value of the e.ColumnIndex property if you want to apply this formatting only so some rcolumns.
     if (e.Value != null) {
         e.Value = e.Value.ToString().ToUpper();
         e.FormattingApplied = true;
     }
}

Solution 2

The easy way to edit cells in upper case is add 'EditingControlShowing' event in your DataGridView.

In this event, you can set the 'CharacterCasing' property, in the control that coming with the DataGridViewEditingControlShowingEventArgs parameter.

This Control is based in the Textbox, so you can work like a TextBox!

If the type of column is difrent from DataGridViewTextBoxColumn, the base of control probably has the property 'CharacterCasing'.

I hope, I have help you.

Italo

Share:
28,264
Jooj
Author by

Jooj

Updated on July 09, 2022

Comments

  • Jooj
    Jooj almost 2 years

    I have a custom DataGridView control and want to set the text format for custom columns in the designer (CellStyle builder).

    Let's say I want to make the text format to uppercase. After searching about this I've found some solutions with adding new events and then changing the text format but this is not what I want. I want to add a new property to all designed columns and there set or change the text format.

    How to do this?

    Thank and best regards.