开发者

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

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 ther开发者_开发知识库e set or change the text format.

How to do this?

Thank and best regards.


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;
     }
}


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


Use this Simple Method in DataGridView EditingControlShowing "Event"

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    If TypeOf e.Control Is TextBox Then
        DirectCast(e.Control, TextBox).CharacterCasing = CharacterCasing.Upper
    End If
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜