C# 如何通过添加新属性将 DataGridView 列文本格式设置为大写?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2001754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:33:09  来源:igfitidea点击:

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

c#.netdatagridviewdatagridviewtextboxcell

提问by Jooj

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

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

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.

谢谢和最好的问候。

采纳答案by Jay Riggs

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 MSDNshould get you started.

如果您真的不想使用各种 DGV 事件来设置文本格式,您始终可以创建自己的 DGV 组件来执行您想要的操作,并使用这些组件代替标准的 DGV 组件。 MSDN 上的这篇文章应该可以帮助您入门。

EDIT

编辑

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

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

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.

将此代码包含在您的项目中。完成此操作后,您将能够向 DataGridViewUpperCaseTextBoxColumn 类型的 DataGridView 添加新的 DataGridViewColumn。这个新的 DataGridViewColumn 将列的 TextBox 组件中输入的所有文本大写。

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:

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

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

回答by Italo

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

编辑大写单元格的简单方法是在 DataGridView 中添加“EditingControlShowing”事件。

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

在此事件中,您可以在带有 DataGridViewEditingControlShowingEventArgs 参数的控件中设置“CharacterCasing”属性。

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'.

如果列的类型与 DataGridViewTextBoxColumn 不同,则控件的基础可能具有属性“CharacterCasing”。

I hope, I have help you.

我希望,我有帮助你。

Italo

伊塔洛

回答by Santosh

Use this Simple Method in DataGridView EditingControlShowing "Event"

在 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