1

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

我这样定义列:

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

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

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属性。

有什么帮助吗?

4

1 回答 1

1

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

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

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;
            }
        }
于 2018-01-29T08:11:28.930 回答