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ó
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.
Hi SZL,
Please find the response for your requirement below,
|
Requirements |
Response |
|
|
|
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,
Note:
|
|
|
|
You can
change the auto-generated column type by using SfDataGrid.AutoGeneratingColumn
event shown below,
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.
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
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; { 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.
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.