开发者

Updating text of a button in a datagridview

I have a DataGridView on a winform开发者_StackOverflow社区. I am dynamically adding DatagridViewButtonColumn in the load method of form with button name as btnAction and text displayed on it as "Process".

So, every row in the grid would have this Process button in the last column.

On click event of this button, I am using a BackgroundWorker to call a method which does some calculations. Once the calculations are over, I need to update that clicked button's text as "Processed" in that row in the grid. How do I achieve this please?

Thanks.


You can set the Value of the cell in question and this will update the text.

For this to work you must have also set the UseColumnTextForButtonValue property of the column to false;

dataGridView[0,0].Value = "Processed";

With that code you just need to change the column and row indexes to match your cell, or access the cell in some other way.

This code sets your value but as you said you are doing this in a background worker you will need a little bit more code to set the text.

The backgroundworker has an event RunWorkerCompleted which gets fired once the worker has completed. In your worker DoWork event handler just have your processing code:

void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    // Some long running processing
}

Then in your RunWorkerCompleted handler you have the setting of the value:

void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    dataGridView1[0,0].Value = "Processed";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜