Click here to Skip to main content
15,882,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to draw graph in zed graph , which required double[] x, double[] y. I want to take value from data column nd put in double[]x and double[]y. dtarow[0] value put in double[] x , and datarow[1] value put in double[]y. I get row[0] and row[1] from store procedure in dataset.
C#
DataSet dsGZData = cls.GetDataSP("spReport_GZData_Current");
      DataTable dtGZData = new DataTable();


      dtGZData = dsGZData.Tables[0];
      dgv_GZData.DataSource = dtGZData;

      foreach (DataRow dtrow in dtGZData.Rows)
      {
          foreach (Data Column dc in dtGZData.Columns)
          {
              list1.Add(dtrow[dc]);
              for (int i = 0; i < list1.Count; i++)
              {
                  lbl_point.Text = list1[i].ToString();

             }

          }

      }
Posted
Comments
ArunRajendra 17-Jul-14 3:03am    
what does row[0] and row[1] contain?
Dipika Wani 17-Jul-14 3:07am    
row[0] is must b x axis, nd row[1] must b y-axis
ArunRajendra 17-Jul-14 3:08am    
Is it single value?
Dipika Wani 17-Jul-14 3:11am    
no its dynamic multiple value
ArunRajendra 17-Jul-14 3:13am    
Can you give a saimple data?

Try This. The code is not test compile or tested there are chances of getting compilation or runtime error. The code is not optimized as well.

C#
double []x = ConvertToArray(row[0]);
double []y = ConvertToArray(row[0]);

private double[] ConvertToArray(string s)
{

       string []s1 = s.split(",");
       double []x= new double[s1.length];              
       for(i=0;i<s1.length;i++)>
       {
           x[i]=convert.ToDouble(s1[i]);
       }
       return x;
}
 
Share this answer
 
Comments
Dipika Wani 17-Jul-14 4:20am    
Thanks for solution..but tell me how to get y[i]?
I didnt get this solution...please explain in brif
Dipika Wani 17-Jul-14 4:49am    
@arjunRajendra - please reply me
ArunRajendra 17-Jul-14 4:49am    
We are converting the string to array.
Dipika Wani 17-Jul-14 5:13am    
get error - Error 5 'System.Array' does not contain a definition for 'length' and no extension method 'length' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?) D:\Dipika\VS2010\Collective Documents\StabilityScreens\StabilityScreens\frmReport.cs
ArunRajendra 17-Jul-14 5:53am    
As I told the code is not compiled. So there could be error due to case. Use intelesense to get the correct case. If length doesn't exist the check for count.
int totalColumns = dtGZData.Columns.Count;
double[] x = new double[totalColumns];
double[] y = new double[totalColumns];

for(int index=0; index<totalcolumns;>{
x[index] = Convert.ToDouble(row[0][index].ToString());
y[index] = Convert.ToDouble(row[1][index].ToString());
}
 
Share this answer
 
Comments
Dipika Wani 18-Jul-14 5:29am    
@ SharmAnuj- I coued try ur code but it gives me error for row[0][index] that Error
""Cannot apply indexing with [] to an expression of type 'object' ""
I remove d index then output show only 1 and 2 column value of row 1
Sharmanuj 18-Jul-14 6:11am    
Hi Deepika,

here is the updated code. instead of row[0] kindly use rowX which holds 1st row object from datatable.

int totalColumns = dtGZData.Columns.Count;
rowX = dtGZData.Rows[0];
rowY = dtGZData.Rows[1];
double[] x = new double[totalColumns];
double[] y = new double[totalColumns];

for(int index=0; index
Dipika Wani 18-Jul-14 8:09am    
row[0][index] is not possibal.....plz help
Use this updated code

C#
int totalColumns = dtGZData.Columns.Count;
rowX = dtGZData.Rows[0];
rowY = dtGZData.Rows[1];
double[] x = new double[totalColumns];
double[] y = new double[totalColumns];

for(int index=0; index<totalcolumns; index++)
{
    x[index] = Convert.ToDouble(rowX[index].ToString());
    y[index] = Convert.ToDouble(rowY[index].ToString());
}
 
Share this answer
 
Comments
Dipika Wani 18-Jul-14 8:14am    
which type of datatype I required for rowX & rowY
Dipika Wani 18-Jul-14 8:20am    
@shamArjun-again rowX[index] give me same error
Sharmanuj 18-Jul-14 9:48am    
Hi Dipika,

could you please share your code, so that i can look into this. \


Regards,
Anuj Sharma
Sharmanuj 18-Jul-14 9:29am    
Hi Dipika,

rowX and rowY would be type of DataRow

i.e
DataRow rowX = dtGZData.Rows[0];
DataRow rowY = dtGZData.Rows[1];
Hi Deepika,

please see the updated and tested code.

DataTable dtData;

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                GetData();
                double[] arrX, arrY;
                arrX = new double[dtData.Columns.Count];
                arrY = new double[dtData.Columns.Count];

                DataRow dRowX, dRowY;
                dRowX = dtData.Rows[0];
                dRowY = dtData.Rows[1];

                for (int x=0; x < dtData.Columns.Count; x++)
                {
                    arrX[x] = Convert.ToDouble(dRowX[x].ToString());
                    arrY[x] = Convert.ToDouble(dRowY[x].ToString());
                }
            }
            catch (Exception ex)
            { }
        }

        private void GetData()
        {
            try
            {
                dtData = new DataTable();
                dtData.Columns.Add("val0", typeof(string));
                dtData.Columns.Add("Val1", typeof(string));

                DataRow dr;
                dr = dtData.NewRow();
                dr[0] = "0.1";
                dr[1] = "0.5";
                dtData.Rows.Add(dr);

                dr = dtData.NewRow();
                dr[0] = "1.1";
                dr[1] = "1.5";
                dtData.Rows.Add(dr);

            }
            catch (Exception ex)
            { }
        }
 
Share this answer
 

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