Search code examples
c#datarow

C# DataRow RowFilter return incorrect row count


I have a windows service that is running every 1 minute to check on a device with the amount of transaction the device made.

The code works perfectly to check if device exist and also to check device name.

However , there is 1 part where number of Rows is checked but not making sense.

The amount of transaction is always the Total Transaction instead of per devices when the RowFilter is already correct. Picture below is for reference where i am checking if my Variables are correct and indeed it is. enter image description here

I have tried different method on filtering the Rows in my datatable.

Attempt 1 :

int i = 0;
foreach (DataRow row in HRDT.Rows)
{
string temp = HRDT.Rows[i].Field<string>(HRDT.Columns.IndexOf("HRDevices"));
iDT.DefaultView.RowFilter = "serialno = " + temp;
i++;
}

Attempt 2 :

foreach (DataRow row in HRDT.Rows)
{

HRDevice = row["HRDevices"].ToString();
iDT.DefaultView.RowFilter = "serialno = " + HRDevice ;

}
iDT.DefaultView.RowFilter = "serialno = " + temp;

None of these gives the correct result. Result should be showing the total rows of by each Device , not the Total Rows of ALL Devices.

I believe i am wrong in my method of row filter and thus seek assistance.

Current full code :

iDT = Ingressds.Tables["ytlhpfprec"];
            int i = 0;
            foreach (DataRow row in HRDT.Rows)
            {

                HRDevice = row["HRDevices"].ToString();
                HRDName = row["HRDName"].ToString();
                string temp = HRDT.Rows[i].Field<string>(HRDT.Columns.IndexOf("HRDevices"));
                LMTF.LogMessageToFile("Device serial is : "+ HRDevice);

                try
                {
                    iDT.DefaultView.RowFilter = "serialno = " + HRDT.Rows[i].Field<string>(HRDT.Columns.IndexOf("HRDevices"));
                    LMTF.LogMessageToFile("Device serial is : "+ HRDevice + " And temp is " + temp);
                    int ipRows = iDT.Rows.Count;
                    string ipString = ipRows.ToString();
                    LMTF.LogMessageToFile(HRDName + " have " + ipString + " transaction.");
                }
                catch (System.Exception ex)
                {
                    LMTF.LogMessageToFile("Error in getting Ingress transaction in Property  : " + HRDName + " " + ex.Message);
                }
                i++;
            }

            Ingressds.Dispose();
            Ingressds.Clear();

Solution

  • DefaultView is a property of the iDT, which returns a DataView. But you're not referring to that DataView instance, you're referring to the iDT instance. If you modify your code to work with view, then it may be more apparent. You can use view.Count to return the number of records in that DataView:

    try
    {
        DataView view = new DataView(iDT);
        view.RowFilter = "serialno = " + temp);
        LMTF.LogMessageToFile("Device serial is : "+ HRDevice + " And temp is " + temp);
        int ipRows = view.Count;
        string ipString = ipRows.ToString();
        LMTF.LogMessageToFile(HRDName + " have " + ipString + " transaction.");
    }