积极答复者
委托操作DataGridView控件

问题
答案
-
使用类似代码:
public void ChangeColor()
{
if(InvokeRequired)
{
if(你的条件)
{
this.Invoke(new MethodInvoke(()=>{dataGridView1.Rows[索引].Cells[索引].Style.BackColor=Color.Red;dataGridView1.Rows[索引].Cells[索引].Style.SelectionForeColor=Color.Green;}));
}
}
else
{
dataGridView1.Rows[索引].Cells[索引].Style.BackColor=Color.Red;dataGridView1.Rows[索引].Cells[索引].Style.SelectionForeColor=Color.Green;
}
}
然后用Thread调用即可。
Thread t = new Thread(ChangeColor);
t.IsBackGround=true;
t.Start();
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats- 已建议为答案 Jason Dot WangModerator 2013年3月21日 9:51
- 已编辑 ThankfulHeartModerator 2013年3月21日 9:52
- 已标记为答案 Jason Dot WangModerator 2013年3月27日 8:27
-
delegate void SetDataGridViewCallback(string s); private void SetDataGrid(string s) { if (this.dataGridView1.InvokeRequired) { SetDataGridViewCallback d = new SetDataGridViewCallback(SetDataGrid); this.Invoke(d, new object[] { s }); } else { for (int i = 0; i < dataGridView1.RowCount; i++) { for (int j = 0; j < dataGridView1.ColumnCount; j++) { if (dataGridView1[j, i].Value.ToString() == s.ToUpper()) { dataGridView1[j, i].Style.BackColor = Color.DeepSkyBlue; dataGridView1[j, i].Style.SelectionBackColor = Color.DeepSkyBlue; } } } } }
另一个自己写的类中定义了,这些
public delegate void SendDataGridValue(string v); public event SendDataGridValue SendDGValue;
然后再使用这个
实例化后.SendDGValue += new 类名.SendDataGridValue(SetDataGrid);
其实我想要的就是这个,本来写对了,以为是这样写有错就来提问题,结果问题出在其他地方。
努力~
- 已标记为答案 ThankfulHeartModerator 2013年3月26日 1:24
- 已编辑 ThankfulHeartModerator 2013年3月26日 1:24 和技术不相干的话语。
- 已编辑 zjyh16 2013年3月26日 1:26
-
hello,
建议使用.net内建的委托来处理跨线程的物件,这样可以让你节省一些代码
//update UI this.Invoke((Action)(() => { //TODO:处理主线程的控件 }));
秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
- 已标记为答案 Jason Dot WangModerator 2013年3月27日 8:27
全部回复
-
使用类似代码:
public void ChangeColor()
{
if(InvokeRequired)
{
if(你的条件)
{
this.Invoke(new MethodInvoke(()=>{dataGridView1.Rows[索引].Cells[索引].Style.BackColor=Color.Red;dataGridView1.Rows[索引].Cells[索引].Style.SelectionForeColor=Color.Green;}));
}
}
else
{
dataGridView1.Rows[索引].Cells[索引].Style.BackColor=Color.Red;dataGridView1.Rows[索引].Cells[索引].Style.SelectionForeColor=Color.Green;
}
}
然后用Thread调用即可。
Thread t = new Thread(ChangeColor);
t.IsBackGround=true;
t.Start();
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats- 已建议为答案 Jason Dot WangModerator 2013年3月21日 9:51
- 已编辑 ThankfulHeartModerator 2013年3月21日 9:52
- 已标记为答案 Jason Dot WangModerator 2013年3月27日 8:27
-
感谢MVP的回答,虽然不是我所需求的,而且最终自行解决问题了。还是很感激!谢谢!
哦?可以Share一下你的答案帮助更多的人吗?并且可以标记答案;)
努力~
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats -
delegate void SetDataGridViewCallback(string s); private void SetDataGrid(string s) { if (this.dataGridView1.InvokeRequired) { SetDataGridViewCallback d = new SetDataGridViewCallback(SetDataGrid); this.Invoke(d, new object[] { s }); } else { for (int i = 0; i < dataGridView1.RowCount; i++) { for (int j = 0; j < dataGridView1.ColumnCount; j++) { if (dataGridView1[j, i].Value.ToString() == s.ToUpper()) { dataGridView1[j, i].Style.BackColor = Color.DeepSkyBlue; dataGridView1[j, i].Style.SelectionBackColor = Color.DeepSkyBlue; } } } } }
另一个自己写的类中定义了,这些
public delegate void SendDataGridValue(string v); public event SendDataGridValue SendDGValue;
然后再使用这个
实例化后.SendDGValue += new 类名.SendDataGridValue(SetDataGrid);
其实我想要的就是这个,本来写对了,以为是这样写有错就来提问题,结果问题出在其他地方。
努力~
- 已标记为答案 ThankfulHeartModerator 2013年3月26日 1:24
- 已编辑 ThankfulHeartModerator 2013年3月26日 1:24 和技术不相干的话语。
- 已编辑 zjyh16 2013年3月26日 1:26
-
hello,
建议使用.net内建的委托来处理跨线程的物件,这样可以让你节省一些代码
//update UI this.Invoke((Action)(() => { //TODO:处理主线程的控件 }));
秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
- 已标记为答案 Jason Dot WangModerator 2013年3月27日 8:27