我在datagridview中添加了更新按钮,当在c#中单击按钮时如何更改它上面的文本?

时间:2012-08-31 12:46:28

标签: c# text datagridview

我正在用c#开发一个Windows应用程序。我使用以下代码

在datagridview中创建了一个按钮
  DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
  dataGridViewTrial.Columns.Add(btn);
  btn.HeaderText = "Update";
  btn.Name = "btn";
  btn.Text = "Update";
  btn.UseColumnTextForButtonValue = true;

现在我想在更新按钮被点击时将btn的文本更改为“保存”。我也想更新我的桌子。我没有得到它。请帮助我:(

2 个答案:

答案 0 :(得分:0)

如果我没有错,你应该这样做

    //Here you add event to button
    void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (e.Control is Button)
        {
            Button btn = e.Control as Button;
            btn.Click -= new EventHandler(btn_Click);
            btn.Click += new EventHandler(btn_Click);
        }
    }

    void btn_Click(object sender, EventArgs e)
    {
        if(sender is button)
            ((button)sender).Text = "new text";
    }

我希望这个帮助

答案 1 :(得分:0)

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                if (e.Control is Button)
                {
                    Button btn = e.Control as Button;

                    // hook or unhook click event here
                }
            }