更新datagridview中按钮的文本 [英] Updating text of a button in a datagridview

查看:145
本文介绍了更新datagridview中按钮的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个winform上有一个DataGridView。我正在动态地添加DatagridViewButtonColumn的形式的加载方法,按钮名称为btnAction,文本显示为Process。



所以,网格中的每一行都将此过程按钮在最后一列。



在此按钮的单击事件上,我正在使用BackgroundWorker来调用一种进行某些计算的方法。计算结束后,我需要将该点击按钮的文本更新为网格中该行的已处理。如何实现?



谢谢。

解决方案

可以设置有问题的单元格的值,这样会更新文本。



为此,您还必须设置 UseColumnTextForButtonValue 属性的列为false;

  dataGridView [0,0] .Value =Processed; 

使用该代码,您只需要更改列和行索引以匹配您的单元格,或访问



此代码设置您的价值,但正如您所说的这样做在后台工作人员中,您将需要一些更多的代码来设置文本



后台工作人员有一个事件 RunWorkerCompleted ,一旦工作完成,它将被触发。在您的工作人员中 DoWork 事件处理程序只需要处理代码:

  void backgroundWorker1_DoWork(对象发件人,DoWorkEventArgs e)
{
//一些长时间运行的处理
}

然后在您的RunWorkerCompleted处理程序中,您的设置值为:

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


I have a DataGridView on a winform. 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";
}

这篇关于更新datagridview中按钮的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆