简体   繁体   中英

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#. I have created a button inside datagridview using following code

  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:(

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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