DateRow conversion

4 posts, 0 answers
  1. Kevin
    Kevin avatar
    26 posts
    Member since:
    Apr 2009

    Posted 26 Jun 2009 Link to this post

    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.
  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 27 Jun 2009 Link to this post

    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.
  3. Kevin
    Kevin avatar
    26 posts
    Member since:
    Apr 2009

    Posted 28 Jun 2009 Link to this post

    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.
  4. Ves
    Admin
    Ves avatar
    2926 posts

    Posted 01 Jul 2009 Link to this post

    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.
Back to Top