更改按钮文本

时间:2013-01-21 10:06:07

标签: c#-4.0 datagridview windows-applications

我在datagridview中有一个按钮,其文字为Start,所以当我单击时,文本现在应为Stop,当我再次点击时文字应该是开始。

所以我编写了代码,但它对我不起作用

 private void dgvCampaign_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (e.Control is Button)
        {
            Button btn = e.Control as Button;
            if(btn.Text=="Start")
                btn.Text = "Stop";
            else
                btn.Text = "Start";
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试使用CellContentClick event

    private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 2)
        {
            if (dataGridView2[e.ColumnIndex, e.RowIndex].EditedFormattedValue.ToString().Length > 0)
            {
                if (dataGridView2[e.ColumnIndex, e.RowIndex].EditedFormattedValue.ToString() == "Start")
                {
                    dataGridView2[e.ColumnIndex, e.RowIndex].Value = "Stop";
                }
                else
                {
                    dataGridView2[e.ColumnIndex, e.RowIndex].Value = "Start";
                }
            }
            else
                dataGridView2[e.ColumnIndex, e.RowIndex].Value = "Start";

        }
    }