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

Pass Datatable Object to Cell C#

1 Answer 188 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Charles
Top achievements
Rank 1
Charles asked on 15 Jan 2014, 03:04 PM
Hi,

I tried to display some charts in a Grid like the attached picture, so I need to pass the data to the cell and I used following code:

Random r = new Random ();
  
GridServer.Rows.Add ("1");
  
for (int i= 1; i <= 16; i++)
{
   
   DataTable table = new DataTable ();
   table.Columns.Add ("DateTime", typeof (DateTime));
   table.Columns.Add ("Value", typeof (double));
   table.Rows.Add (DateTime.Now, r.Next (100));
   table.Rows.Add (DateTime.Now.AddSeconds (30), r.Next (100));
   table.Rows.Add (DateTime.Now.AddSeconds (60), r.Next (100));
   table.Rows.Add (DateTime.Now.AddSeconds (90), r.Next (100));
   table.Rows.Add (DateTime.Now.AddSeconds (120), r.Next (100));
   table.Rows.Add (DateTime.Now.AddSeconds (150), r.Next (100));
  
   GridServer.Rows[0].Cells[i].Value = table;
  
}

Basing on the definition, the type of GridServer.Rows[0].Cells[i].Value is object

public class GridViewCellInfo : IEquatable<GridViewCellInfo>
   {
 
     // Summary:
      //     Gets or sets the value.
      public object Value { get; set; }
      //
}

So the GridServer.Rows[0].Cells[i].Value should get the reference of my DataTable, but the actually value of Value is a empty string.

Is this a bug or anything wrong in my code?

Thanks

Charles

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 20 Jan 2014, 11:25 AM
Hello Charles,

Thank you for contacting Telerik Support.

In order to display a RadChartView in RadGridView's cell, you may use the following approach.
public Form1()
{
    InitializeComponent();
 
    GridServer.AutoSizeRows = true;
     
    for (int i = 0; i < 5; i++)
    {
        GridViewImageColumn imageColumn = new GridViewImageColumn("Server " + i);
        imageColumn.ImageLayout = ImageLayout.Zoom;          
        GridServer.Columns.Add(imageColumn);
    }
}
 
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
 
    GridViewRowInfo newRow = GridServer.Rows.NewRow();
    foreach (GridViewColumn column in GridServer.Columns)
    {
        RadChartView chart = new RadChartView();
        LineSeries lineSeria = new LineSeries();
 
        chart.Series.Add(lineSeria);
        lineSeria.HorizontalAxis.LabelFitMode = Telerik.Charting.AxisLabelFitMode.MultiLine;
        lineSeria.ValueMember = "Value";
        lineSeria.CategoryMember = "DateTime";
 
        lineSeria.DataSource = GetChartData();
        chart.LoadElementTree();
      
        Bitmap bmp = new Bitmap(chart.Width, chart.Height, PixelFormat.Format32bppArgb);
        chart.DrawToBitmap(bmp, new Rectangle(0, 0, chart.Width, chart.Height));
        newRow.Cells[column.Name].Value = bmp;
    }
 
    GridServer.Rows.Add(newRow);
}
 
private DataTable GetChartData()
{
    Random r = new Random();
     
    DataTable table = new DataTable();
    table.Columns.Add("DateTime", typeof (DateTime));
    table.Columns.Add("Value", typeof (double));
    for (int i = 1; i <= 5; i++)
    {
        table.Rows.Add(DateTime.Now, r.Next(100));
        table.Rows.Add(DateTime.Now.AddSeconds(30), r.Next(100));
        table.Rows.Add(DateTime.Now.AddSeconds(60), r.Next(100));
        table.Rows.Add(DateTime.Now.AddSeconds(90), r.Next(100));
        table.Rows.Add(DateTime.Now.AddSeconds(120), r.Next(100));
        table.Rows.Add(DateTime.Now.AddSeconds(150), r.Next(100));
    }
 
    return table;
}

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Charles
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or