Sam

Sam

  • NA
  • 166
  • 0

DataTable add an array as new row

Feb 25 2014 11:01 AM
I have a long string of values, separated by new lines and commas.
I am trying to convert the array to a DataTable of Doubles.
I keep getting casting error:Error1Cannot implicitly convert type 'double[]' to 'object[]' .
The code and error message are attached.
Can anyone help me out?
Is there a better way to make the conversion?
Thanks
Sam 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Data_Mapping_Control
{
    class temp
    {
        string[] dataRecordsCSV = new string[320];
        public double[] dataRow = null;
        
        public DataTable inDataToTable(string inData)
        {
            DataTable dataTable = new DataTable();

            dataTable.Columns.Add("slot", typeof(double));
            dataTable.Columns.Add("time", typeof(double));
            dataTable.Columns.Add("state", typeof(double));
            dataTable.Columns.Add("difference", typeof(double));
            string[] record = new string[4];
            Double[] dataValues = new Double[4];
            DataRow newRow;
            // Data String: "16, , , \r\n0,21,1,-10\r\n0,100,0,1\r\n1,120,1,1\r\n1,201,0,0\r\n2,220,1,1\r\n2,300,0,1.....
            //Convert the dataString to CSV records array; 
            dataRecordsCSV = inData.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            int col = 0;
            foreach (string s in dataRecordsCSV)
            {
                record = s.Split(',');

                for (col = 0; col < 4; col++)
                    double.TryParse(record[col], out dataValues[col]);
                newRow = dataTable.NewRow().ItemArray = dataValues;  //error: "Cannot implicitly convert type 'double[]' to 'object[]'"
                dataTable.Rows.Add(newRow);
            }
        }
    }
}
 


Answers (4)