Hello, Chris,
RadGridView provides a convenient way for creating custom cells. The possible solution that I can suggest is to create a custom header cell element and add a checkbox in it. Thus, when toggling its state you can perform the desired logic.
I have prepared a sample code snippet for your reference:
private void RadForm1_Load(object sender, EventArgs e)
{
this.productsTableAdapter.Fill(this.nwindDataSet.Products);
this.radGridView1.BestFitColumns();
this.radGridView1.CellFormatting+=radGridView1_CellFormatting;
this.radGridView1.Columns.RemoveAt(1);
CustomColumn customColumn = new CustomColumn();
customColumn.Width = 100;
customColumn.Enabled = true;
customColumn.FieldName = "ProductName";
this.radGridView1.Columns.Insert(1,customColumn);
}
private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
if (e.Column.FieldName=="ProductName" && e.Row is GridViewDataRowInfo)
{
e.CellElement.Enabled = ((CustomColumn)e.Column).Enabled;
}
}
public class CustomColumn : GridViewTextBoxColumn
{
public override Type GetCellType(GridViewRowInfo row)
{
if (row is GridViewTableHeaderRowInfo)
{
return typeof(CustomHeaderCell);
}
return base.GetCellType(row);
}
public bool Enabled { get; set; }
}
public class CustomHeaderCell : GridHeaderCellElement
{
public CustomHeaderCell(GridViewColumn column, GridRowElement row) : base(column, row)
{
}
protected override Type ThemeEffectiveType
{
get
{
return typeof(GridHeaderCellElement);
}
}
public override bool IsCompatible(GridViewColumn data, object context)
{
return data is CustomColumn;
}
RadCheckBoxElement checkBox = new RadCheckBoxElement();
protected override void CreateChildElements()
{
base.CreateChildElements();
this.checkBox.CheckStateChanged += checkBox_CheckStateChanged;
checkBox.StretchHorizontally = true;
this.Children.Add(checkBox);
}
private void checkBox_CheckStateChanged(object sender, EventArgs e)
{
CustomColumn column = this.ColumnInfo as CustomColumn;
if (column != null)
{
column.Enabled = this.checkBox.Checked;
this.GridControl.MasterTemplate.Refresh();
}
}
protected override void SetContentCore(object value)
{
base.SetContentCore(value);
this.DrawText = false;
this.checkBox.CheckStateChanged -= checkBox_CheckStateChanged;
CustomColumn column = this.ColumnInfo as CustomColumn;
if (column != null)
{
this.checkBox.Text = column.FieldName;
this.checkBox.Checked = column.Enabled ;
}
this.checkBox.CheckStateChanged += checkBox_CheckStateChanged;
}
}
I hope this information helps. If you need any further assistance please don't hesitate to contact me.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Progress is here for your business, like always.
Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.