更改DataGridViewButtonColumn中按钮的文本值 [英] Change the text value of a button in DataGridViewButtonColumn

查看:692
本文介绍了更改DataGridViewButtonColumn中按钮的文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次尝试单击按钮时,我都试图更改DataGridViewButtonColumn中按钮的文本.

I'm trying to change the text of a button in a DataGridViewButtonColumn every time the button is clicked.

我这样定义列:

DataGridViewButtonColumn sitemapButtonColumn = new DataGridViewButtonColumn
{
     Name = "Process",
     Text = "Start",
     UseColumnTextForButtonValue = true,
     DataPropertyName = "Process",
     FillWeight = 7,
     Width = 75
};
dg_list.CellContentClick += dg_list_StartStopProcessClick;

现在,单击单元格后控制事件的功能是:

Now the function which controls the event once a cell clicked is:

private void dg_list_StartStopProcessClick(object sender, DataGridViewCellEventArgs e)
        {
            var senderGrid = (DataGridView)sender;
            if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
                e.RowIndex >= 0 &&
                e.ColumnIndex == dg_lista_blogs_automatizacion.Columns["Process"].Index)
            {
                if (senderGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "Start")
                {
                    senderGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "Stop";
                    senderGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.OrangeRed;
                }
                else
                {
                    senderGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "Start";
                    senderGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.White;
                }
            }
        }

嗯,这行不通!

我一直在搜索,发现有一条帖子将UseColumnTextForButtonValue修改为false,设置了新的文本值,然后再次设置为true.

I've been googling and found a post which modifies UseColumnTextForButtonValue to false, set the new text value and set to true again.

问题是我无法弄清楚如何在事件中访问 UseColumnTextForButtonValue 属性.

The problem is that i can not figure out how can i get access to UseColumnTextForButtonValue property within the event.

有帮助吗?

推荐答案

基本上,我可以解决在 DataGridViewButtonColumn 实例中将 UseColumnTextForButtonValue 设置为false的问题.

Basically, i could solve the problem setting UseColumnTextForButtonValue to false in the DataGridViewButtonColumn instance.

UseColumnTextForButtonValue 设置为false时,必须在其他情况下初始化按钮的文本值.因此,我使用 CellFormatting 事件根据我想要的状态设置文本值.

When UseColumnTextForButtonValue is set to false, the text value of the button must be initied in other event. So, i use CellFormatting event for setting the text value according to the state I desire.

private void dg_list_auto_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e!=null && e.ColumnIndex==0)
            {
                dg_list_auto.Rows[e.RowIndex].Cells[0].Value = dg_list_auto[e.RowIndex].flag_sitemap_started?"Stop":"Start";
                dg_list_auto.Rows[e.RowIndex].Cells[0].Style.BackColor = dg_list_auto[e.RowIndex].flag_sitemap_started ? Color.IndianRed: Color.GreenYellow;
            }
        }

这篇关于更改DataGridViewButtonColumn中按钮的文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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