Search code examples
c#ndde

how to make Advise loop for items in a DataTable and show realtime values on dataGridView


I have a DataTable filled with data, there are columns called "ALIASNUM" and "VALUE". I'm trying to get the values for all items in "ALIASNUM" column and put it in "VALUE" column.

this is my code:

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;
}

This code is working fine, but I want to use StartAdvise to make these values a realtime values and show the datatable on dataGridView with realtime values, how can I do this?


this is the full code after using startadvise:

1- handler for advise

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 Connection

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

Solution

  • Here's your setup method re-worked:

    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...
    }
    

    And here's a handler for advise:

    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?)
    }
    

    Other considerations:

    1) If dataTable1 is derived through SQL calls, it will be slow.

    2) You'll be at the mercy of grid refresh, so handle that where needed.

    3) Always think about types. DDE is sending type information and raw Bytes...use it where you can.