如何通过添加新属性将DataGridView列文本格式设置为大写? [英] How to set DataGridView columns text format to uppercase by adding new property?

查看:256
本文介绍了如何通过添加新属性将DataGridView列文本格式设置为大写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义DataGridView控件,并且想要在设计器(CellStyle构建器)中为自定义列设置文本格式.

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.

该怎么做?

感谢和问候.

推荐答案

恐怕没有标准的属性来格式化所需的文本.

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

如果您确实不想使用各种DGV事件来进行文本格式设置,则可以始终创建自己的DGV组件来执行所需的操作,并使用这些组件来代替标准DGV组件. 有关MSDN的文章应该使您入门.

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.

编辑

这是博客条目来自一个自称HanSolo的人,他可以满足您的需求.

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

代码如下:

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

将此代码包含在您的项目中.完成后,您将可以向DataGridViewUpperCaseTextBoxColumn类型的DataGridView添加新的DataGridViewColumn.这个新的DataGridViewColumn将在该列的TextBox组件中输入的所有文本都大写.

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.

您还应该重新考虑不使用事件的决定.这很容易做到.例如,如果您有一个名为dataGridView1的DGV,则可以使用如下所示的CellFormatting事件:

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

这篇关于如何通过添加新属性将DataGridView列文本格式设置为大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆