Change column type of auto generated column

Hello,

I have an SfDataGrid control with auto generated columns. The column header text is coming from data annotations.

I would like change the column type programatically while every other settings (headertext, tooltip etc.) remaining the original.

Can you help me please, how can I do this?

For example change the auto generated Checkbox column to three state Checkbox column, but keep the header text auto generated from data annotations.

My another question is how can I replace the auto generated column with my custom control column and editor control? How will the custom control get the header text from the data annotations (like other auto generated column)?

Thank you!

BR, László


6 Replies 1 reply marked as answer

DM Dhanasekar Mohanraj Syncfusion Team July 15, 2022 06:34 PM UTC


Hi SZL,

Currently, we are checking the feasibilities to achieve your requirement. We need two more business days to validate this. We will validate and update you on the details on July 19, 2022.

We appreciate your patience until then.

Regards,
Dhanasekar M.



DM Dhanasekar Mohanraj Syncfusion Team July 20, 2022 02:36 PM UTC

Hi SZL,

Please find the response for your requirement below,

Requirements

Response


Change the auto generated Checkbox column to three state Checkbox column, but keep the header text auto generated from data annotations.


 

Your requirement to change the auto-generated Checkbox column to three state checkbox columns in SfDataGrid can be achieved by using SfDataGrid.AutoGeneratingColumn event shown below,

 

this.sfDataGrid1.AutoGeneratingColumn += SfDataGrid1_AutoGeneratingColumn;

private void SfDataGrid1_AutoGeneratingColumn(object sender, Syncfusion.WinForms.DataGrid.Events.AutoGeneratingColumnArgs e)

{

    if (e.Column.MappingName == "Status")

    {

        (e.Column as GridCheckBoxColumn).AllowThreeState = true;

    }

}

 

 

Note:
          If you need to change the GridCheckColumn’s checkboxes as three state check-boxes the underlying business property should be nullable boolean type.

 

 

 


My another question is how can I replace the auto generated column with my custom control column and editor control? How will the custom control get the header text from the data annotations (like other auto generated column)?


 

You can change the auto-generated column type by using SfDataGrid.AutoGeneratingColumn event shown below,

this.sfDataGrid1.AutoGeneratingColumn += SfDataGrid1_AutoGeneratingColumn;


private void SfDataGrid1_AutoGeneratingColumn(object sender, Syncfusion.WinForms.DataGrid.Events.AutoGeneratingColumnArgs e)

{

    if (e.Column.MappingName == "OrderID" && e.Column is GridNumericColumn)

    {

        e.Column = new GridTextColumn() { MappingName = e.Column.MappingName , HeaderText = e.Column.HeaderText };

    }

}


For more information related to changing the column type, please refer to the below user guide documentation link,

UG Link: https://help.syncfusion.com/windowsforms/datagrid/columns#changing-column-type

Note:

         If you change the column type you should reset the header text and other required properties in the AutoGeneratingColumnArgs event.


Please find the sample in the attachment and let us know if you have any concerns about this.


Regards,
Dhanasekar M.


Attachment: SfDataGridDemo_b9ade736.zip

Marked as answer

SZ SZL replied to Dhanasekar Mohanraj July 21, 2022 08:03 PM UTC

Hi,

Thank you, its works well.

I have only one more question:

So I change a CheckBoxColumn to TextBoxColumn following your example.

In this case a "True" or "False" message appear in grid at runtime.

How can I change the text? ("True" -> "On", "False" -> "Off")


EDIT:

I try create a Custom Format Provider and set to column, but not happen anything:


 public class BooleanFormatProvider : IDataGridFormatProvider

    {

        public object Format(string format, GridColumnBase gridColumn, object record, object value)

        {

            if (value == null)

            {

                return "-";

            }


            if (gridColumn is GridColumn)

            {

                bool v = bool.Parse(value.ToString());

                return (v ? "On" : "Off");

            }


            return "?";

        }



        public object GetFormat(Type formatType)

        {

            return this;

        }


        public string Format(string format, object arg, IFormatProvider formatProvider)

        {

            throw new NotImplementedException();

        }

    }


 private void PrimaryGrid_AutoGeneratingColumn(object sender, AutoGeneratingColumnArgs e)

        {

            if (e.Column.MappingName == nameof(AnyagrendelesListItemDto.Nyomtatva))

            {

                e.Column = new GridTextColumn() { MappingName = e.Column.MappingName, HeaderText = e.Column.HeaderText, FormatProvider = new BooleanFormatProvider() };

            }

        }


Thank you very much!

BR, SZL



DM Dhanasekar Mohanraj Syncfusion Team July 22, 2022 04:33 PM UTC

Hi SZL,

Your requirement to load the GridTextColumn with On/Off when based on the GridCheckBoxColumns value can be achieved with the help of SfDataGrid.DrawCell event shown below,

this.sfDataGrid1.DrawCell += OnSfDataGrid1_DrawCell;

private void OnSfDataGrid1_DrawCell(object sender, DrawCellEventArgs e)

{

    if(e.Column.MappingName == "Status" && e.Column is GridTextColumn)

    {

        if (e.DisplayText == "True")

            e.DisplayText = "On";

        else

            e.DisplayText = "Off";

    }       

}


Please find the sample in the attachment and let us know if you have any concerns about this.


Regards,

Dhanasekar M.


Attachment: SfDataGridDemo_2a621009.zip


SZ SZL replied to Dhanasekar Mohanraj July 23, 2022 03:21 PM UTC

Thank you, its working!



DM Dhanasekar Mohanraj Syncfusion Team July 25, 2022 01:58 PM UTC

Hi SZL,

If you are satisfied with our response, please mark it as an answer. Otherwise, please let us know if you have any further queries on this. We are happy to help you.

Regards,

Dhanasekar M.


Loader.
Up arrow icon