将按钮列添加到DataGridView并将条件文本仅应用于单击的按钮 [英] Add a Button Column to a DataGridView and apply conditional text only to the button clicked

查看:32
本文介绍了将按钮列添加到DataGridView并将条件文本仅应用于单击的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在DataGridView中,我们可以处理行级别的按钮文本更改吗?只需单击按钮即可将文本更改为"单击",其余行上的其他按钮保留默认文本.

我添加了按钮的一个实例,并将其添加到网格中,如下所示:

  var button = new DataGridViewButtonColumn();{button.Name ="statusButton";button.HeaderText ="Button";button.Text =点击注册";button.UseColumnTextForButtonValue = true;this.dataGridView1.Columns.Add(button);} 

但是当我处理私有void dataGridView1_CellContentClick(对象发送者,DataGridViewCellEventArgs e)事件中的名称更改时,所有按钮的文本均更改为"Clicked".

我是否需要为 n 行数创建 n 个按钮?

解决方案

如文档中所述,

做同一件事的方法有很多,这是一种简单直接的方法,应该可以解决当前问题.

In a DataGridView, can we handle the button text change in the row level? Only the clicked buttons have to change the text to 'Clicked', other buttons on remaining rows keeps default text.

I added one instance of the button and add that to the grid, like below:

var button = new DataGridViewButtonColumn();
{
    button.Name = "statusButton";
    button.HeaderText = "Button";
    button.Text = "Click to register";
    button.UseColumnTextForButtonValue = true;  
    this.dataGridView1.Columns.Add(button);
}

but when I handle the name change in the private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) event all the buttons were changed the text to 'Clicked'.

Do I need to have the n number of buttons created for the n number of rows?

解决方案

As describe in the documentation, the UseColumnTextForButtonValue is used to specify that all Buttons of a DataGridViewButtonColumn will use the same Text, as specified in the Text property of the Column's template.

If you need to specify a different text for each button and change it at run-time, you need to set this property to false (or don't set it, since false is the default).
In this case, the FormattedValue of the Cell is used for the Text.

Since you're adding the Column at run-time, you may not have a corresponding value in the DataSource (the Column is not bound to the source of data).
In this case, the Cell.Value will be null, so the FormattedValue property will return an empty string and your Buttons will show an empty text.

To set a default value, you can then set the Column's DefaultCellStyle.NullValue to a predefined string, which will then used as the Text for the Buttons.

var buttonColumn = new DataGridViewButtonColumn() {
    Name = "statusButton",
    HeaderText = "Button",
    UseColumnTextForButtonValue = false,
    DefaultCellStyle = new DataGridViewCellStyle() {
        NullValue = "Click Me"
    }
};
this.dataGridView1.Columns.Add(buttonColumn);

Then you can change the text of a single Button Cell, set a new Value and, eventually, compare this value with the [Cell].OwningColumn.DefaultCellStyle.NullValue, if you want, e.g., to re-set it:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1[e.ColumnIndex, e.RowIndex] is DataGridViewButtonCell cell) {
        if (cell.Value == null || cell.Value == cell.OwningColumn.DefaultCellStyle.NullValue) {
            cell.Value = "Clicked";
        }
        else {
            cell.Value = cell.OwningColumn.DefaultCellStyle.NullValue;
        }
    }
}

There are different ways to do the same thing, this is a simple and direct method, it should allow you to solve the current issue.

这篇关于将按钮列添加到DataGridView并将条件文本仅应用于单击的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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