0

我正在用 c# 开发一个 Windows 应用程序。我使用以下代码在 datagridview 内创建了一个按钮

  DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
  dataGridViewTrial.Columns.Add(btn);
  btn.HeaderText = "Update";
  btn.Name = "btn";
  btn.Text = "Update";
  btn.UseColumnTextForButtonValue = true;

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

4

2 回答 2

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";
    }

我希望这有帮助

于 2012-08-31T12:56:54.203 回答
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
                }
            }
于 2012-08-31T12:57:33.417 回答