【问题标题】:Change the text value of a button in DataGridViewButtonColumn更改 DataGridViewButtonColumn 中按钮的文本值
【发布时间】:2018-07-03 23:09:31
【问题描述】:

每次单击按钮时,我都会尝试更改 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 属性。

有什么帮助吗?

【问题讨论】:

    标签: c# datagridviewcolumn datagridviewbuttoncolumn


    【解决方案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;
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2011-07-25
      • 2013-05-11
      • 2022-12-10
      • 1970-01-01
      • 2013-07-31
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 2022-12-04
      相关资源
      最近更新 更多