Solved

C# Datatable to double[]

Posted on 2009-05-12
4
608 Views
Last Modified: 2012-05-06
What is the easiest way to convert a column of data from a datatable to a double[]?  I am trying to populate a componentone chart and can't get the binding to work right so I figured I would try the brute force method and set the chart to double[] and be done with it.  I am trying to go to a List<double> and then from the list to double[] but I get an error when trying to compile the list that
"Error      1      The as operator must be used with a reference type or nullable type ('double' is a non-nullable value type)"

I am open to cleaner ways to do this.
thanks
foreach (DataRow myRow in sim1.Tables["Data"].Rows)
            {
 
                xdata.Add(myRow["Count"] as double);
            }

Open in new window

0
Comment
Question by:perrytaylor
  • 2
4 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 24364908
foreach (DataRow myRow in sim1.Tables["Data"].Rows)
    xdata.Add((double)myRow["Count"]);
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24364945
couple of ways:
 

foreach (DataRow myRow in sim1.Tables["Data"].Rows)
{
    // Check the data for nulls and default to zero if it is null
    xdata.Add( myRow["Count"] == null ? 0 : (double)myRow["count"] );
 
OR
   // use tryparse:
   double tmp = 0;
   double.TryParse(myRow["Count"], out tmp);
   xdata.Add(tmp);
 
}

Open in new window

0
 
LVL 6

Expert Comment

by:HarryNS
ID: 24365045
List<double> lstValues = new List<double>(dt.Rows.Count);
            foreach (DataRow dr in dt.Rows)
            {
                lstValues.Add(double.Parse(dr["Column Name"].ToString()));
            }
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 24365497
GuitarRich raises a good point - unless you know there won't be any null values in your DataSet you should check it first to avoid a runtime Exception.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Loop not working 29 53
Error on Add method 1 50
C# HTTP GET method sample code 3 55
How come this regular expression allows "e"? 14 34
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now