简体   繁体   English

我在datagridview中添加了更新按钮,当在C#中单击按钮时,如何更改文本?

[英]i have added update button inside datagridview how to change text on it when button is clicked in c#?

I am developing a windows application in c#. 我正在用C#开发Windows应用程序。 I have created a button inside datagridview using following code 我使用以下代码在datagridview内创建了一个按钮

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

now i want to change the text of btn to "save" when the update button get clicked.Also i want to update my table.I m not getting it.Please help me:( 现在我想在单击更新按钮时将btn的文本更改为“保存”。我也想更新我的表。我没有得到它。请帮助我:(

If im not mistake you should do this 如果我没记错,您应该这样做

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

I hope this help 我希望这个帮助

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

                    // hook or unhook click event here
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM