[英]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.