我正在用c#开发一个Windows应用程序。我使用以下代码
在datagridview中创建了一个按钮 DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
dataGridViewTrial.Columns.Add(btn);
btn.HeaderText = "Update";
btn.Name = "btn";
btn.Text = "Update";
btn.UseColumnTextForButtonValue = true;
现在我想在更新按钮被点击时将btn的文本更改为“保存”。我也想更新我的桌子。我没有得到它。请帮助我:(
答案 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
}
}