Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using a common method to convert DataTable to List<dictionary<string,object>>.
I am getting some float values in DataTable, values are correct but on putting loop on DataTable to fetch values, I first get DataRow. This is the time where my values gets changed.

Eg. Value in DataTable cell is 947.8 but while retrieving it from DataRow it gives something like 947.80000000007.

What I have tried:

This my common function used for conversion.
C#
public static List<Dictionary<string, object>> ToList(DataTable dtable)
        {
            Dictionary<string, object> dict = null;
            List<Dictionary<string, object>> lookUpData = new List<Dictionary<string, object>>();

            try
            {
                foreach (DataRow drow in dtable.Rows)
                {
                    dict = new Dictionary<string, object>();
                    for (int intCol = 0; intCol <= dtable.Columns.Count - 1; intCol++)
                    {
                        if (!string.IsNullOrEmpty(drow[intCol].ToString().Trim()))
                        {
                            dict.Add(dtable.Columns[intCol].ColumnName.Trim(), drow[intCol]);
                        }
                        else
                        {
                            dict.Add(dtable.Columns[intCol].ColumnName.Trim(), null);
                        }
                    }
                    lookUpData.Add(dict);

                }


            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lookUpData;
        }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900