New to Telerik UI for WinForms? Download free 30-day trial

RadGridView – RadChartView integration

Date Posted Product Author
January 07, 2015 Telerik UI for WinForms Desislava Yordanova

Problem

This article demonstrates how to implement integration between RadGridView and RadChartView.

radgridview-radchartview-integration 001

Solution

1. First, let's populate the RadGridView with data. For this purpose, we will bind it to the NorthWind.OrderDetails table.

2. You should enable multiple selection in RadGridView by setting its MultiSelect property to true. Additionally, you need to change the SelectionMode property to CellSelect. Thus, you will be able to select specific grid cells and populate the RadChartView with the respective data.

this.radGridView1.MultiSelect = true;
this.radGridView1.SelectionMode = Telerik.WinControls.UI.GridViewSelectionMode.CellSelect;

3. Subscribe to the RadGridView.SelectionChanged event and generate a BarSeries for each distinct column that participates in the grid’s selection. Afterwards, add the cell value as a *CategoricalDataPoint *to the series:

private void radGridView1_SelectionChanged(object sender, EventArgs e)
{
    if (this.radGridView1.SelectedCells.Count > 0)
    {
        PopulateChart(this.radGridView1.SelectedCells);
    }
}

private void PopulateChart(GridViewSelectedCellsCollection gridViewSelectedCellsCollection)
{
    List<string> orderIds = new List<string>();
    this.radChartView1.Series.Clear();
    this.radChartView1.Axes.Clear();
    this.radChartView1.ShowLegend = true;

    foreach (GridViewCellInfo cell in gridViewSelectedCellsCollection)
    {
        double cellValue;
        if (double.TryParse(cell.Value + "", out cellValue))
        {
            BarSeries barSeries;
            DataRowView rowView = cell.RowInfo.DataBoundItem as DataRowView;
            if (!orderIds.Contains(rowView.Row["OrderID"].ToString()))
            {
                orderIds.Add(rowView.Row["OrderID"].ToString());
                barSeries = new BarSeries();
                barSeries.Name = rowView.Row["OrderID"].ToString();
                barSeries.LegendTitle = barSeries.Name;

                this.radChartView1.Series.Add(barSeries);
            }
            else
            {
                barSeries = GetBarSeries(rowView.Row["OrderID"].ToString()) as BarSeries ;
            }
            barSeries.DataPoints.Add(new CategoricalDataPoint(cellValue, cell.ColumnInfo.Name));
        }
    }

    this.radChartView1.Invalidate();
}

private ChartSeries GetBarSeries(string p)
{
    foreach (ChartSeries s in this.radChartView1.Series)
    {
        if (s.Name == p)
        {
            return s;
        }
    }

    return null;
}

A complete solution in C# and VB.NET can be found here.

In this article