Solved

C# Datatable to double[]

Posted on 2009-05-12
4
606 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
Comment Utility
foreach (DataRow myRow in sim1.Tables["Data"].Rows)
    xdata.Add((double)myRow["Count"]);
0
 
LVL 12

Expert Comment

by:GuitarRich
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

6 Experts available now in Live!

Get 1:1 Help Now