Customize cell editor depending on the cell value in radgrid in windows form application with c#.

1 Answer 59 Views
GridView
Amol
Top achievements
Rank 1
Amol asked on 01 Aug 2023, 05:22 AM

Hello

I am working on windows form application with c#.

I have used telerik radgrid on the form.

Need a checkbox in the radgrid cell depending on the value of the cell.

e.g. If the value in the cell is true or false then instead of showing true or false in the cell need to checkbox,

if the value in the cell is date or datetime then datetimepicker should display in the cell.

Here is the screenshot describing my problem.

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 02 Aug 2023, 02:32 PM

Hi, Amol,

RadGridView provides a convenient API for specifying what editor to be activated. It is suitable to use the EditorRequired event. The following help article demonstrates a sample approach how to change the editor for a cell:

https://docs.telerik.com/devtools/winforms/controls/gridview/editors/how-to/change-the-active-editor-depending-on-the-cell-value-type 

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.

Amol
Top achievements
Rank 1
commented on 03 Aug 2023, 05:34 AM

Hello thanks for the reply, but sorry to say that it is not working.

 

Dess | Tech Support Engineer, Principal
Telerik team
commented on 07 Aug 2023, 10:58 AM

Hi, Amol,

I have prepared a sample project for your reference. Please give it a try and see how it works on your end. 

        public RadForm1()
        {
            InitializeComponent();

            GridViewTextBoxColumn gridViewTextBoxColumn1 = new GridViewTextBoxColumn();
            GridViewTextBoxColumn gridViewTextBoxColumn2 = new GridViewTextBoxColumn();
            gridViewTextBoxColumn1.HeaderText = "column1";
            gridViewTextBoxColumn1.Name = "column1";
            gridViewTextBoxColumn1.Width = 100;
            gridViewTextBoxColumn2.HeaderText = "column2";
            gridViewTextBoxColumn2.Name = "column2";
            gridViewTextBoxColumn2.Width = 150;
            this.radGridView1.MasterTemplate.Columns.AddRange(new GridViewDataColumn[]
            {
                gridViewTextBoxColumn1,
                gridViewTextBoxColumn2
            });
            this.radGridView1.Rows.Add("row 1", DateTime.Now.ToString());
            this.radGridView1.Rows.Add("row 2", "6");
            this.radGridView1.Rows.Add("row 3", "test");
            this.radGridView1.Rows.Add("row 4", true);
            this.radGridView1.Rows.Add("row 5", false);
            this.radGridView1.EditorRequired += radGridView1_EditorRequired;
            this.radGridView1.CellEndEdit+=radGridView1_CellEndEdit;
        }

        private void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
        {
            if (e.ActiveEditor is RadCheckBoxEditor)
            {
                e.Row.Cells[e.Column.Name].Value = (ToggleState)e.ActiveEditor.Value == ToggleState.On ? "True" : "False";
            }
        }

        void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
        {
            bool boolValue = false;
            if (bool.TryParse(radGridView1.CurrentCell.Value.ToString(), out boolValue))
            {
                e.EditorType = typeof(RadCheckBoxEditor);
                return;
            }
        }

 

The observed result is illustrated below:

Should you have further questions please let me know.

Amol
Top achievements
Rank 1
commented on 22 Aug 2023, 11:01 AM

Hello Dess,

As you can see in the above image that when user clicks on the cell then it is showing the checkbox, but I want to show the checkbox without clicking on the cell(at the time of loading the data).

Dess | Tech Support Engineer, Principal
Telerik team
commented on 22 Aug 2023, 12:08 PM

Hi, Amol,

If you don't want to enter edit mode and activate the editor at all, this means that you want to display different elements inside the cells belonging to the same column. The possible solution is to create one common cell element and add all the possible elements (RadButtonElement or LightVisualElement, etc.) that you will need to show in the visual cell. Then, you will need to manage the Visibility of each element considering your custom condition indicating which element to be shown for the row in the SetContentCore method of cell element.

A sample approach is demonstrated here:

https://docs.telerik.com/devtools/winforms/knowledge-base/custom-gridview-cells-conditional-elements 

Please give it a try and see how it would work for your scenario.

 

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