Hello,
I use a event View_CreateCell to set custom filter cell element but the event not fire for all columns in grid. How can I change that?
private void ListaPlikowRadGridView_CreateCell(object sender, GridViewCreateCellEventArgs e)
{
if (e.CellType == typeof(GridSearchCellElement))
{
e.CellType = typeof(MyGridSearchCellElement);
}
if (e.CellType == typeof(GridFilterCellElement) && e.Column.GetType() != typeof(GridViewTextBoxColumn))
{
e.CellElement = new CustomGridFilterCellElement(e.Column as GridViewDataColumn, e.Row);
}
}
3 Answers, 1 is accepted
Hello, Zygmunt,
Due to the UI virtualization in RadGridView, cell elements are created only for currently visible cells and they are being reused during operations like scrolling, filtering, grouping and so on. That is why the CreateCell event may not be fired for all columns.
Usually, a better option is to create a custom column, override its GetCellType method and specify what cell element to be used according to the row type. A sample approach is demonstrated in the following help article:
https://docs.telerik.com/devtools/winforms/controls/gridview/cells/creating-custom-cells
I hope this information helps. If you need any further assistance please don't hesitate to contact me.
Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik
Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.
In the CreateCell event you need to create a custom GridFilterCellElement for the desired columns and use a custom default GridFilterCellElement for the rest of the columns. In order to deal with the cells reusing when scrolling, you need to specify that your custom GridFilterCellElement is compatible only with particular columns and the default GridFilterCellElement should be compatible with the rest of them. I have prepared a sample code snippet for your reference:
public RadForm1()
{
InitializeComponent();
this.radGridView1.CreateCell += radGridView1_CreateCell;
}
private void radGridView1_CreateCell(object sender, GridViewCreateCellEventArgs e)
{
if (e.CellType == typeof(GridFilterCellElement))
{
if (e.Column.GetType() != typeof(GridViewTextBoxColumn))
{
e.CellElement = new CustomGridFilterCellElement(e.Column as GridViewDataColumn, e.Row);
}
else //default filter cell for the rest of the columns
{
e.CellElement = new DefaultGridFilterCellElement(e.Column as GridViewDataColumn, e.Row);
}
}
}
private void RadForm1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'nwindDataSet.Products' table. You can move, or remove it, as needed.
this.productsTableAdapter.Fill(this.nwindDataSet.Products);
this.radGridView1.DataSource = this.productsBindingSource;
this.radGridView1.BestFitColumns();
this.radGridView1.EnableFiltering = true;
}
public class CustomGridFilterCellElement : GridFilterCellElement
{
public CustomGridFilterCellElement(GridViewDataColumn column, GridRowElement row) : base(column, row)
{
}
protected override Type ThemeEffectiveType
{
get
{
return typeof(GridFilterCellElement);
}
}
protected override void CreateChildElements()
{
base.CreateChildElements();
RadButtonElement btn = new RadButtonElement();
btn.Text = "Click";
this.Children.Add(btn);
}
public override bool IsCompatible(GridViewColumn data, object context)
{
//compatible with different than text columns
return data.GetType() != typeof(GridViewTextBoxColumn);
}
}
public class DefaultGridFilterCellElement : GridFilterCellElement
{
public DefaultGridFilterCellElement(GridViewDataColumn column, GridRowElement row) : base(column, row)
{
}
protected override Type ThemeEffectiveType
{
get
{
return typeof(GridFilterCellElement);
}
}
public override bool IsCompatible(GridViewColumn data, object context)
{
//compatible with text columns
return data.GetType() == typeof(GridViewTextBoxColumn);
}
}
https://docs.telerik.com/devtools/winforms/knowledge-base/gridview-permanenteditor-in-filter-cell
Feel free to use this approach which suits your requirements best.
Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.
public class CustomGridFilterCellElement : GridFilterCellElement
{
protected override Type ThemeEffectiveType
{
get
{
return typeof(GridFilterCellElement);
}
}
public CustomGridFilterCellElement(GridViewDataColumn column, GridRowElement row)
: base(column, row)
{
this.FilteringRowInfo.SuspendPropertyNotifications();
this.SetFilterOperator(Telerik.WinControls.Data.FilterOperator.IsEqualTo);
this.FilteringRowInfo.ResumePropertyNotifications();
}
public override bool IsCompatible(GridViewColumn data, object context)
{
return true;
}
}
public class CustomGridViewDateTimeColumn : GridViewDateTimeColumn
{
public override Type GetCellType(GridViewRowInfo row)
{
if (row is GridViewFilteringRowInfo)
{
return typeof(CustomGridFilterCellElement);
}
return base.GetCellType(row);
}
}
public class CustomGridViewDecimalColumn : GridViewDecimalColumn
{
public override Type GetCellType(GridViewRowInfo row)
{
if (row is GridViewFilteringRowInfo)
{
return typeof(CustomGridFilterCellElement);
}
return base.GetCellType(row);
}
}
It looks like the UI virtualization may be causing this issue. Please have in mind that the cell elements in the grid are reused. With incorrect configuration of the custom cells, you may end up with custom cells in wrong columns. There is an easy fix and it is the IsCompatible method of the CustomGridFilterCellElement. Please override it and make sure to return true only for the CustomGridViewDecimalColumn and if you are in the header row.
More information on the UI Virtualization: https://docs.telerik.com/devtools/winforms/controls/gridview/fundamentals/ui-virtualization. Example with a custom cell and column, please note step 5: https://docs.telerik.com/devtools/winforms/controls/gridview/cells/creating-custom-cells.