首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >更改DataGridViewButtonColumn中按钮的文本值

更改DataGridViewButtonColumn中按钮的文本值
EN

Stack Overflow用户
提问于 2018-01-24 19:39:13
回答 1查看 2.1K关注 0票数 1

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

我对列的定义如下:

代码语言:javascript
运行
复制
DataGridViewButtonColumn sitemapButtonColumn = new DataGridViewButtonColumn
{
     Name = "Process",
     Text = "Start",
     UseColumnTextForButtonValue = true,
     DataPropertyName = "Process",
     FillWeight = 7,
     Width = 75
};
dg_list.CellContentClick += dg_list_StartStopProcessClick;

现在,在单元格单击后控制事件的函数是:

代码语言:javascript
运行
复制
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。

问题是,我不知道如何在事件中访问UseColumnTextForButtonValue属性。

有什么帮助吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-29 08:11:28

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

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

代码语言:javascript
运行
复制
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;
            }
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48430385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档