New to Telerik UI for WinFormsStart a free 30-day trial

GridViewSparklineColumn

Updated over 6 months ago

GridViewSparklineColumn allows a Sparkline chart to be displayed in the RadGridView. GridViewSparklineColumn can be one of the following types: SparkAreaSeries, SparkBarSeries, SparkLineSeries, SparkScatterSeries, SparkWinLossSeries.

WinForms RadGridView GridViewSparklineColumn

Add GridViewSparklineColumn to the grid.

The following code snippet demonstrates how to create and add GridViewSparklineColumn to RadGridView with some sample data added in it:

C#
 GridViewSparklineColumn sparkLineColumn = new GridViewSparklineColumn();
 sparkLineColumn.SeriesType = SparkSeriesType.Line;
 sparkLineColumn.Name = "Line";
 sparkLineColumn.FieldName = "Line";
 sparkLineColumn.HeaderText = "SparkLineColumn";
 sparkLineColumn.ShowHighPointIndicator = true;
 sparkLineColumn.ShowFirstPointIndicator = true;
 sparkLineColumn.ShowLastPointIndicator = true;
 sparkLineColumn.ShowNegativePointIndicators = true;
 this.radGridView1.Columns.Add(sparkLineColumn);

     GridViewRowInfo row = this.radGridView1.Rows.AddNew();
     double[] values = new double[12];
     for (int i = 0; i < values.Length; i++)
     {
         values[i] = rand.Next(-100, 100);
     }
     row.Cells["Line"].Value = values;

     row = this.radGridView1.Rows.AddNew();
     values = new double[12];
     for (int i = 0; i < values.Length; i++)
     {
         values[i] = rand.Next(-100, 100);
     }
     row.Cells["Line"].Value = values;

     row = this.radGridView1.Rows.AddNew();
     values = new double[12];
     for (int i = 0; i < values.Length; i++)
     {
         values[i] = rand.Next(-100, 100);
     }
     row.Cells["Line"].Value = values;

     row = this.radGridView1.Rows.AddNew();
     values = new double[12];
     for (int i = 0; i < values.Length; i++)
     {
         values[i] = rand.Next(-100, 100);
     }
     row.Cells["Line"].Value = values;

     row = this.radGridView1.Rows.AddNew();
     values = new double[12];
     for (int i = 0; i < values.Length; i++)
     {
         values[i] = rand.Next(-100, 100);
     }
     row.Cells["Line"].Value = values;

SparkDataNeeded event

The SparkDataNeeded event can be used to dynamically populate or change data at run-time. It is fired right before creating the data points inside the sparkline element in the cell.

C#
 private void SparkLineColumn_SparkDataNeeded(object sender, SparkDataNeededEventArgs e)
 {
     GridSparklineCellElement gridSparklineCellElement = sender as GridSparklineCellElement;
     if (gridSparklineCellElement == null)
     {
         return;
     }

     var myValues = new double[6];
     for (int i = 0; i < 6; i++)
     {
         myValues[i] = rand.Next(-100, 100);
     }

     e.Values = myValues;
 }
 

Binding GridViewSparklineColumn

You can bind the GridViewSparklineColumn to any IEnumerable collection of numeric data types. To do this, you should set the FieldName of the GridViewSparklineColumn to be the same as the name of the appopriate existing column in the data source. The example below binds the grid to DataTable via the DataSource property. To prevent the grid from traversing all data fields in that collection, I set the GridViewTemplate.AutoGenerateColumns property to False.

C#
DataTable dt = new DataTable();
dt.Columns.Add("Id",typeof(int));
dt.Columns.Add("Name",typeof(string));
dt.Columns.Add("Points", typeof(double[]));

Random rand = new Random();
for (int i = 0; i < 5; i++)
{
    double[] pointsValues = new double[12];
    for (int j = 0; j < pointsValues.Length; j++)
    {
        pointsValues[j]=rand.Next(-100, 100);
    }
   
    dt.Rows.Add(i,"Row"+i, pointsValues);
}

this.radGridView1.MasterTemplate.AutoGenerateColumns = false;
this.radGridView1.DataSource = dt;

GridViewSparklineColumn sparkLineColumn = new GridViewSparklineColumn("Points");
sparkLineColumn.SeriesType = SparkSeriesType.Line;
sparkLineColumn.FieldName = "Points";
sparkLineColumn.HeaderText = "Points";
this.radGridView1.Columns.Add(sparkLineColumn);

Customize GridViewSparklineColumn

GridViewSparklineColumn can be customized by making use of CellFormatting event. Here is an example how to make the even rows in GridViewSparklineColumn to desplay bar series, and the odd rows to display line series. Due to the UI Virtualization, an if-else statement is used to reset the series.

WinForms RadGridView GridViewSparklineColumn Customize

C#
private void RadGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    GridViewSparklineColumn sparklineColumn = e.CellElement.ColumnInfo as GridViewSparklineColumn;
    if (sparklineColumn != null)
    {
        GridSparklineCellElement sparklineCellElement = sender as GridSparklineCellElement;
        if (sparklineCellElement == null || sparklineCellElement.SparklineElement.Series == null)
        {
            return;
        }

        SparkCartesianSeries series = null;
        if (e.CellElement.RowIndex % 2 == 0)
        {
            series = new SparkBarSeries();
        }
        else
        {
            series = new SparkLineSeries();
        }

        series.ShowFirstPointIndicator = sparklineColumn.ShowFirstPointIndicator;
        series.ShowLastPointIndicator = sparklineCellElement.SparklineElement.Series.ShowLastPointIndicator;
        series.ShowHighPointIndicator = sparklineCellElement.SparklineElement.Series.ShowHighPointIndicator;
        series.ShowLowPointIndicator = sparklineCellElement.SparklineElement.Series.ShowLowPointIndicator;
        series.ShowNegativePointIndicators = sparklineCellElement.SparklineElement.Series.ShowNegativePointIndicators;

        SparkDataPoint[] values = new SparkDataPoint[sparklineCellElement.SparklineElement.Series.DataPoints.Count];
        sparklineCellElement.SparklineElement.Series.DataPoints.CopyTo(values, 0);
        sparklineCellElement.SparklineElement.Series.DataPoints.Clear();

        series.DataPoints.AddRange(values);
        sparklineCellElement.SparklineElement.Series = series;
    }
}

GridViewSparklineColumn's Properties

PropertyDescription
SeriesTypeGets or sets a value indicating what is the type of the generated series.
ShowTooltipGets or sets a value whether tooltips on the point markers will be shown.
ShowMarkersGets or sets a value indicating whether the point markers will be shown.
ShowHighPointIndicatorGets or sets a value indicating whether the high point marker will be shown.
ShowLowPointIndicatorGets or sets a value indicating whether the low point marker will be shown.
ShowFirstPointIndicatorGets or sets a value indicating whether the first point marker will be shown.
ShowLastPointIndicatorGets or sets a value indicating whether the last point marker will be shown.
ShowNegativePointIndicatorsGets or sets a value indicating whether the negative points marker will be shown.
AllowSearchingGets or sets a value indicating whether the user can search by this column.
AllowFilteringGets or sets a value indicating whether the user can filter by this column.

GridViewSparklineColumn's Events

EventDescription
PaintSparkFillFires when a spark fill will be painted using a specific solid brush.
PaintSparkStrokeFires when a spark line will be painted using a specific Pen.
SparkDataNeededFires before adding data points to the SparkSeries. It is possible to cancel the event and bind the series.
SparklineElementExportingFires while exporting or printing the sparkline cell element, allows customization of the series.