I need to show check box , text and date time in same column in different rows based on a value received from Database.

3 Answers 106 Views
GridView
Shubham
Top achievements
Rank 3
Iron
Iron
Iron
Shubham asked on 26 Oct 2023, 01:16 PM

So we are populating rad grid from data which we get from database.
my requirement is like based on datatype which we get from service(database) I need to show checkbox or datetime or text in same column.
if I will be receiving 1 or 0 I need to show checkbox marked as checked / unchecked respectively if got text than simple text need to be shown.
pls check below image for more reference.

Requirement

 What we tried:
so I am here sharing you my the things we tried.
Actually we tried using cell formatting event to change the cell element based on data it is working fine but we are experiencing visual glitch's.
glitch is like have checkbox on all columns of same row as we scroll right or left in grid.

Note: on the first time its work fine but when we scroll than glitches come.

I am attaching the code snippet which we tried also attaching video clip which will show the glitches.

Code snippet:

private void GrdHistory2_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.CellElement is GridDataCellElement)
    {
        // Assuming you have a data source where the column "MixedTypeData" determines the cell type
        if (e.Column.Name == grdHistoryColNewValue.Name)
        {
            if (e.Row.Cells["New Value"].Value != null)
            {
                if (e.Row.Cells["New Value"].Value.ToString() == "0")
                {
                    e.CellElement.Children.Clear();
                    RadCheckBoxElement checkBoxElement = new RadCheckBoxElement();
                    e.CellElement.Children.Add(checkBoxElement);
                }
                else if(e.Row.Cells["New Value"].Value.ToString() == "1")
                {
                    e.CellElement.Children.Clear();
                    RadCheckBoxElement checkBoxElement = new RadCheckBoxElement();
                    e.CellElement.Children.Add(checkBoxElement);
                }
            }
        }
    }
}

pls check below gif image. 
Here I just need to show in
column "New Value" but it also coming on all other column while scrolling. disappear after while .
Please suggest me some solution so that it would work for my use case. 

Thanks,
Shubham Jain

3 Answers, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 27 Oct 2023, 06:47 AM

Hello, Shubham,

It is a common case to have a single column (for example a GridViewTextBoxColumn) with different values in the rows. By default, RadGridView activates an appropriate editor for the column to handle the cell's value changes. You can see the default editors listed here: Default Editors - WinForms GridView Control - Telerik UI for WinForms

RadGridView offers a mechanism for changing the default editor depending on the cell value type. The editor can be replaced by using the EditorRequired event. Please refer to the following documentation article that I believe would be quite helpful in achieving your goal: Change the Active Editor Depending on the Cell Value Type. - RadGridView - Telerik UI for WinForms

In your case, you can use RadCheckBoxEditor for the boolean values, RadDateTimeEditor for the DateTime values, and RadTextBoxEditor for the text in the same column.

I hope this will be useful. If you have any other questions, do not hesitate to ask.

Regards,
Nadya
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.

Shubham
Top achievements
Rank 3
Iron
Iron
Iron
commented on 27 Oct 2023, 07:38 AM

Hi Nadya,

I think you not got our requirement actually grid is not in a Edit mode editor required will be fired when users click on that(grid must be in edit mode).
In our case data is been coming from database we are populating that in a grid so based on that data i need to show check box if i get bool type value or simple text if I receive string type.
Please let us know if you need anything more from our side.

Thanks,
Shubham Jain
0
Nadya | Tech Support Engineer
Telerik team
answered on 27 Oct 2023, 09:47 AM

Hello, Shubham,

Please excuse me for the misunderstanding in my previous reply. It is my fault that I actually forgot to explain how to set up the grid in order to show a check box initially when the data is populated in grid. Indeed, the part about using custom editors will be useful at a later stage if you want to allow editing those values. 

Now, in order to show a check box in the cell if the value from the data source is of type bool you should create a custom column that uses a custom cell. The custom column will use only one cell element. However, this cell element will contain all possible elements, e.g. checkbox, text, numeric value, etc. In the SetContentCore method of the custom cell, you will determine which of the elements will be visible and which elements should be hidden according to the type of received value. The following help article demonstrates a sample approach how to create custom cells: Creating Custom Cells - WinForms GridView Control - Telerik UI for WinForms

I have prepared a sample project for achieving your goal. You can find the attached project on how to create a custom cell and display for some of the rows a checkbox and for the rest of the plain text. Note, in the CreateChildElements you can use any RadElement that is suitable for your needs. Feel free to extend this sample in a way to achieve your custom requirements.

I hope this helps. Please let me know if you have other questions.

Regards,
Nadya
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.

Shubham
Top achievements
Rank 3
Iron
Iron
Iron
commented on 03 Nov 2023, 12:21 PM

Hi Nadya,

Is there any other way than a custom cell. Because we have already made custom cell one time which was hosting our custom control but we were facing glitches in that as well so do not want to repeat that.

Please Help!

Thanks,
Shubham Jain

0
Nadya | Tech Support Engineer
Telerik team
answered on 06 Nov 2023, 01:56 PM

Hello, Shubham,

In order to achieve your goal where different elements are shown in the cell from the same column you should create a custom cell. When using a custom cell it is important to carefully override its IsCompatible and SetCotentCore methods in order to work properly. Because of the UI virtualization mechanism that RadGridView uses, only the currently visible cells are created and they are further reused when needed. A cell element is reused only if it is compatible for them. Once the custom cell is correctly set up, no visual glitches would appear.

Let me know if you have any other questions.

Regards,
Nadya
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.

Tags
GridView
Asked by
Shubham
Top achievements
Rank 3
Iron
Iron
Iron
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or