This is a migrated thread and some comments may be shown as answers.

DateRow conversion

3 Answers 81 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 27 Jun 2009, 03:21 AM
Hi,

Based in the intergration sample I am trying to populate a chart from a grid. I have the following functions which I took from your sample. I have changed the function to use all records not just the selected ones.

       private void ShowSelectedRecordsInChart()  
        {  
 
            DataTable table = new DataTable();  
 
            DataColumn col = new DataColumn("Shuttle"typeof(double));  
 
            table.Columns.Add(col);  
 
            try 
            {  
                foreach (DataRecord item in this.datagrdV02.Records)  
                {  
                    DataRow row = table.NewRow();  
 
                    row["Shuttle"] = ((DataRow)item.Data)["Shuttle"];  
 
                    table.Rows.Add(row);  
                }  
            }  
            catch (Exception ex)  
            {  
                MessageBox.Show(ex.Message);  
            }   
        } 

When I execure the following line:

row[

"Shuttle"] = ((DataRow)item.Data)["Shuttle"];

 


I get the following error:

Unable to cast object of type 'CoachTech.Test_VO2' to type 'System.Data.DataRow'.

Any Idea what I am doing wrong?

Cheers,
Kevin.

3 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 27 Jun 2009, 05:28 AM
Hi Kevin,

Most probably the grid is not bound to DataTable and that is why you cannot case DataRecord.Data to DataRow.

Best wishes,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Kevin
Top achievements
Rank 1
answered on 28 Jun 2009, 07:30 AM
Hi,

The grid is populated using the following code...

                var Results = from a in db.Test_VO2s  
                              where a.AthleteID == AthleteID  
                              select new 
                              {  
                                  a.Date,  
                                  a.Shuttle,  
                                  a.VO2,  
                                  a.Max_Heart_Rate,  
                                  a.Level  
                              };  
 
 
                this.datagrdV02.ItemsSource = Results; 

so as you can see its not bound to a DataTable. I have tried to get the Linq result to populate a DataTable but cant seem to get it ro work.

and Ideas?

I have set the chart ItemSoucrce to the Results var as well and that populates the chart but I will lose the intergration.

Also I wanted to set the Date column from the query as the lable across the X axis. How do I do this?

Cheers,
Kevin.
0
Ves
Telerik team
answered on 01 Jul 2009, 12:50 PM
Hi Kevin,

The value of e.Item.DataItem is an object, which corresponds to that row in the grid. If the datasource is a DataTable e.Item.DataItem will hold a DataRow. If the datasource is a list of custom objects e.Item.DataItem will hold an instance of that custom object type.

That said, if your code looked like this:
var Results = from a in db.Test_VO2s   
     where a.AthleteID == AthleteID   
     select a ; 
  
this.datagrdV02.ItemsSource = Results;  
you would get that message and you could simply cast e.Item.DataItem to Test_VO2. However, your code snippet shows, that you use an anonymous type, so you would not be able to cast e.Item.DataItem. A possible solution would be to use the above code and a line like this:

row["Shuttle"] = ((Test_VO2)(e.Item.DataItem).Shuttle;

Alternatively, you can create your own type, containing only the properties you need (Date, Shuttle, VO2, Max_Heart_Rate and Level) and use it like this:

var Results = from a in db.Test_VO2s    
     where a.AthleteID == AthleteID    
     select new MyType 
     { 
         Date = a.Date, Shuttle = a.Shuttle,...... 
     } 

Hope this helps.

Greetings,
Ves
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Chart
Asked by
Kevin
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Kevin
Top achievements
Rank 1
Ves
Telerik team
Share this question
or