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

发布于 2024-08-17 10:41:36 字数 196 浏览 1 评论 0原文

我有一个自定义 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.

How to do this?

Thank and best regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

獨角戲 2024-08-24 10:41:36

恐怕没有标准属性可以按照您想要的方式格式化文本。

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

编辑

这是来自自称 HanSolo 的博客条目,它可以满足您的需求。

代码如下:

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 组件中输入的所有文本大写。

您还应该重新考虑不使用事件的决定。这很容易做到。例如,如果您有一个名为 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;
     }
}

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;
     }
}
家住魔仙堡 2024-08-24 10:41:36

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

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

该控件基于文本框,因此您可以像文本框一样工作!

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

我希望,我对你有帮助。

伊塔洛

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

昔梦 2024-08-24 10:41:36

在显示“事件”的 DataGridView EditingControl 中使用此简单方法

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文