All,
How do I convert a DataTable double from a row object to a normal double?
I'm creating the DataTable like this (note that Temp and Hum are doubles):
private void readDummyData(DataTable dt)
{
string fileName = "dummydata.csv";
string[] lineCSV = new string[5];
StreamReader myReader = new StreamReader(fileName);
// Define data table
dt.Columns.Add("TimeStamp", Type.GetType("System.DateTime"));
dt.Columns.Add("Location", Type.GetType("System.String"));
dt.Columns.Add("Temp", Type.GetType("System.Double"));
dt.Columns.Add("Hum", Type.GetType("System.Double"));
dt.Columns.Add("Status", Type.GetType("System.String"));
string line;
while ((line = myReader.ReadLine()) != null)
{
lineCSV = line.Split(new char[] { ',' });
DataRow dr = dt.NewRow();
dr["TimeStamp"] = lineCSV[0];
dr["Location"] = lineCSV[1];
dr["Temp"] = lineCSV[2];
dr["Hum"] = lineCSV[3];
dr["Status"] = lineCSV[4];
dt.Rows.Add(dr);
}
myReader.Close();
}
And I'm trying to use the data like this:
// Build X and Y arrays
int i = 0;
foreach (DataRow row in dataTable.Rows)
{
xData[i] = i;
tempData[i] = double.Parse(row[2] + "");
humData[i] = double.Parse(row[3] + "");
tempLowerLimitData[i] = tempMin;
tempUpperLimitData[i] = tempMax;
humLowerLimitData[i] = humMin;
humUpperLimitData[i] = humMax;
i++;
}
Note the funky double.Parse(row[2] + "")
. That's the only way I could turn the row object into a string so that I could double.Parse
it. (row[2].ToString()
doesn't work.)
row[2] and row[3] are already doubles, but when I do this tempData[i] = row[2]
I get the following error meaasge: Error 1 Cannot implicitly convert type 'object' to 'double'. An explicit conversion exists (are you missing a cast?)
It says that an explicit version exists, but I can't figure out what that is.
Anyone have an answer?
Thanks,
-Bill