How to Create Custom Cells with Conditional Elements
Environment
| Product Version | Product | Author |
|---|---|---|
| 2020.1.218 | RadGridView for WinForms | Desislava Yordanova |
Description
RadGridView provides a convenient approach for creating custom cell elements. A common requirement is show different elements inside the cells depending on certain conditions.
This article demonstrates how to show either a RadProgressBarElement or a RadWaitingBarElement considering a custom property's (ProgressMode) value of the column.

Solution
Create a derivative of the GridViewDataColumn and introduce a ProgressMode property that will control whether the RadProgressBarElement or the RadWaitingBarElement will be shown inside the cells. Then, override the GetCellType method and specify the type of the GridDataCellElement that will be used for the respective row type.
In the custom GridDataCellElement it is necessary to add all the elements that you will need to show in the cell. However, in the SetContentCore method you will manage the Visibility of each element considering your custom condition, e.g. the column's ProgressMode in this case.
Please refer to the complete code snippet:
public RadForm1()
{
InitializeComponent();
GridViewProgressColumn col = new GridViewProgressColumn();
col.ProgressMode = ProgressStyle.Waiting;
this.radGridView1.Columns.Add(col);
for (int i = 0; i < 10; i++)
{
this.radGridView1.Rows.Add(i * 1.25 * 8);
}
this.radGridView1.TableElement.RowHeight = 50;
this.radGridView1.TableElement.RowSpacing = 1;
}
enum ProgressStyle
{
Progress,
Waiting
}
class GridViewProgressColumn : GridViewDataColumn
{
public GridViewProgressColumn() : base()
{
}
public override Type GetCellType(GridViewRowInfo row)
{
if (row is GridViewDataRowInfo)
return typeof(GridViewProgressBarCell);
return base.GetCellType(row);
}
public ProgressStyle ProgressMode { get; set; }
}
internal class GridViewProgressBarCell : GridDataCellElement
{
RadProgressBarElement progressBarElement;
RadWaitingBarElement waitingBarElement;
public GridViewProgressBarCell(GridViewColumn column, GridRowElement row) : base(column, row)
{
}
protected override void CreateChildElements()
{
base.CreateChildElements();
progressBarElement = new RadProgressBarElement();
waitingBarElement = new RadWaitingBarElement();
waitingBarElement.WaitingStyle = Telerik.WinControls.Enumerations.WaitingBarStyles.SegmentedRing;
waitingBarElement.WaitingSpeed = 50;
this.Children.Add(progressBarElement);
this.Children.Add(waitingBarElement);
}
protected override void SetContentCore(object value)
{
int intValue = 0;
int.TryParse(value + "", out intValue);
{
progressBarElement.Value1 = intValue;
}
GridViewProgressColumn col = this.ColumnInfo as GridViewProgressColumn;
if (col != null)
{
if (col.ProgressMode == ProgressStyle.Waiting)
{
this.progressBarElement.Visibility = ElementVisibility.Collapsed;
this.waitingBarElement.Visibility = ElementVisibility.Visible;
if (!this.waitingBarElement.IsWaiting)
{
this.waitingBarElement.StartWaiting();
}
}
else
{
this.progressBarElement.Visibility = ElementVisibility.Visible;
this.waitingBarElement.Visibility = ElementVisibility.Collapsed;
this.waitingBarElement.StopWaiting();
}
}
base.SetContentCore(value);
}
}
private void radDropDownList1_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
{
GridViewProgressColumn col = this.radGridView1.Columns[0] as GridViewProgressColumn;
if (e.Position == 0)
{
col.ProgressMode = ProgressStyle.Waiting;
}
else
{
col.ProgressMode = ProgressStyle.Progress;
}
this.radGridView1.MasterTemplate.Refresh();
}