View_CreateCell not fire for all columns

3 Answers 153 Views
GridView
Zygmunt
Top achievements
Rank 1
Iron
Iron
Iron
Zygmunt asked on 20 Sep 2021, 08:07 AM

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

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 20 Sep 2021, 08:18 AM

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.

0
Zygmunt
Top achievements
Rank 1
Iron
Iron
Iron
answered on 20 Sep 2021, 08:41 AM
It's no not perfect solution for my because I already have more then ten columns which I have to set custom filter.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 21 Sep 2021, 12:35 PM
Hello, Zygmunt, 

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);
            }
        }
However, the appropriate way for creating custom filter cell elements is to use a custom column as it is demonstrated in the following KB article:
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/.

Zygmunt
Top achievements
Rank 1
Iron
Iron
Iron
commented on 23 Sep 2021, 10:44 AM

 I already have columns  created in GridViewDataColumn Collection Editor. It is possible to create for example custom datetime column type and change existing datatime column to created custom type?
Hristo
Telerik team
commented on 24 Sep 2021, 09:29 AM

Hi Zygmunt! The collection editors are used inside the Visual Studio designer. Putting your custom column type there will require a lot of effort. Moreover, currently there is no API to allow such customizations. If you want to change the generated default DateTime column with the custom one, it will be necessary to modify the designer file of your form manually. Alternatively, you can add this custom column at runtime.
Zygmunt
Top achievements
Rank 1
Iron
Iron
Iron
commented on 27 Sep 2021, 06:20 AM

Can I change programmatically column type created in VS editor to custom type keeping current column settings?
Hristo
Telerik team
commented on 27 Sep 2021, 09:58 AM

The editor in Visual Studio will only work with the predefined column types, listed here: GridViewDataColumn - WinForms GridView Control. You can change the type of an existing column to your custom type by hand inside your designer file. The other settings, e.g. Width, Name, etc. should not be lost. 
Zygmunt
Top achievements
Rank 1
Iron
Iron
Iron
commented on 28 Sep 2021, 12:51 PM | edited

I created Custom GridFilterCellElement and custom columns like shown below  and change the types in form designer but for some columns my custom filter not set properly. After some work the problematic filters are fixing itself. Generally the problem are columns which are hidden under horizontal scrollbar (ont teh right site of grid).

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);
        }
    }

Hristo
Telerik team
commented on 29 Sep 2021, 11:53 AM | edited

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.

Tags
GridView
Asked by
Zygmunt
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Zygmunt
Top achievements
Rank 1
Iron
Iron
Iron
Share this question
or