0

我有一个填充数据的 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- 开始建议

//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);
    }
4

1 回答 1

0

这是您重新设计的设置方法:

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 正在发送类型信息和原始字节......尽可能使用它。

于 2013-05-28T14:28:24.190 回答