[英]how to make Advise loop for items in a DataTable and show realtime values on dataGridView
我有一个充满数据的DataTable,有称为“ ALIASNUM”和“ VALUE”的列。 我正在尝试获取“ ALIASNUM”列中所有项目的值,并将其放在“ VALUE”列中。
这是我的代码:
NDde.Client.DdeClient client = new NDde.Client.DdeClient("TR1EMCodeEmulator", "Command");
client.Connect();
DataRow[] rowList = dataTable1.Select("ALIASNUM > 0 ");
foreach (DataRow dr in rowList)
{
string alias = dr["ALIASNUM"].ToString();
string currentValue = client.Request(alias, 60000);
dr["VALUE"] = currentValue;
}
这段代码运行良好,但是我想使用StartAdvise将这些值设置为实时值,并在dataGridView上显示具有实时值的数据表,我该怎么做?
这是使用startadvise之后的完整代码:
1-建议的处理程序
private void client1_Advise(Object sender, NDde.Client.DdeAdviseEventArgs e)
{
DataRow[] rowList1 = dataTable1.Select("ALIASNUM = " + e.Item);
if (rowList1.Length > 0)
{
rowList1[0]["VALUE"] = e.Text;
}
}
2- DDE连接
string ddeApplication1 = "";
string ddeTopic1 = "";
ddeApplication1 = "TR1EMCodeEmulator";
ddeTopic1 = "Command";
NDde.Client.DdeClient client1 = new NDde.Client.DdeClient(ddeApplication1, ddeTopic1);
client1.Connect();
3- StartAdvise
//Get the values for the requested data through the DDE connections.
client1.Advise += client1_Advise;
DataRow[] rowList1 = dataTable1.Select("ALIASNUM > 0 ");
foreach (DataRow dr1 in rowList1)
{
string alias = dr1["ALIASNUM"].ToString();
client1.StartAdvise(alias, 1, true, 60000);
}
这是您重新设置的方法:
try
{
NDde.Client.DdeClient client = new NDde.Client.DdeClient("TR1EMCodeEmulator", "Command");
client.Connect();
client.Advise += client_Advise;
DataRow[] rowList = dataTable1.Select("ALIASNUM > 0 ");
foreach (DataRow dr in rowList)
client.StartAdvise(dr["ALIASNUM"] as String, 0, true, 60000); //the zero is DDE type, replace as necessary
}
catch (Exception ex)
{
//Do what you need to here...
}
这是一个建议的处理程序:
private void client_Advise(Object sender, NDde.Client.DdeAdviseEventArgs e)
{
//Tests to consider: Is it the right format? Is it valid? What's the state?
DataTable dt = new DataTable(); //replace with whatever data you *really are updating*
DataRow[] row = dt.Select("ALIASNUM = " + e.Item);
if (row.Length > 0)
row[0]["VALUE"] = e.Text; //could use e.Data and convert from Byte[] to what you need (what is VALUE type?)
}
其他注意事项:
1)如果dataTable1通过SQL调用导出,这将是缓慢的。
2)您将受到网格刷新的支配,因此请在需要时进行处理。
3)总是考虑类型。 DDE正在发送类型信息和原始字节……请尽可能使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.