SfDataGrid bound to data table with AutoGenerateColumns=true formatting numeric columns

Hi,
I have a SfDataGrid bound to a data table and AutoGenerateColumns is set to true.
I want to set NumberOfDecimalDigits and NumberGroupSizes only when the datacolumn type in the data table is decimal/double/float but not for int/long.
How can I achieve this?
I tried the below in the code behind (by setting AutoGenerateColumns to false)
private void EventSearchResultsDataGridOnItemsSourceChanged(object sender, GridItemsSourceChangedEventArgs e)
{
EventSearchResultsDataGrid.Columns.Clear();
var dataTable = e.NewItemsSource as DataTable;
if (dataTable == null)
{
return;
}
foreach (DataColumn dataColumn in dataTable.Columns)
{
GridColumn gridColumn;
if (dataColumn.DataType == typeof(int) || dataColumn.DataType == typeof(long))
{
gridColumn = new GridNumericColumn
{
NumberDecimalDigits = 0
};
}
else if (dataColumn.DataType == typeof(float) || dataColumn.DataType == typeof(decimal) || dataColumn.DataType == typeof(double))
{
gridColumn = new GridNumericColumn
{
NumberDecimalDigits = 3,
NumberGroupSizes = new Int32Collection(new[] {3})
};
}
else if (dataColumn.DataType == typeof(DateTime) || dataColumn.DataType == typeof(DateTimeOffset))
{
gridColumn = new GridDateTimeColumn
{
Pattern = DateTimePattern.CustomPattern,
CustomPattern = "dd MMM yyyy HH:mm:ss"
};
}
else if (dataColumn.DataType == typeof(bool))
{
gridColumn = new GridCheckBoxColumn();
}
else
{
gridColumn = new GridTextColumn();
}
SetColumnMapping(gridColumn, dataColumn);
EventSearchResultsDataGrid.Columns.Add(gridColumn);
}
}
This sorts the display formatting. However, now the Sorting/Filtering/Grouping all fail to work.
Thanks,
Som

7 Replies

JG Jai Ganesh S Syncfusion Team March 8, 2018 03:24 AM UTC

 
Query 1: 
 
You can set the NumberOfDecimalDigits and NumberGroupSizes based on the DataType of that column by using the column MappingName in AutoGeneratingColumn event like below, 
 
void AssociatedObject_AutoGeneratingColumn(object sender, AutoGeneratingColumnArgs e) 
{ 
    else if(e.Column.MappingName== "Id") 
    { 
        (e.Column as GridNumericColumn).NumberDecimalDigits = 0; 
    } 
    else if (e.Column.MappingName == "Salary") 
    { 
        (e.Column as GridNumericColumn).NumberDecimalDigits = 3; 
    } 
 
} 
 
Query 2: 
 
While we give the special character in MappingName, it will not perform the Sorting/Filtering/Grouping based on that. However, you can achieve your requirement to show the special character in Column’s HeaderText instead of MappingName like below, 
 
if (e.Column.MappingName == "Country") 
{ 
   e.Column.HeaderText = "Country/ShipCity"; 
} 
 
 
Regards, 
Jai Ganesh S


SC Somanna Chottekalapanda March 8, 2018 10:11 AM UTC

Hi Jai Ganesh,

I will try the solution for query 1.
I quite didn't understand your reasoning for second query. Do you mean having a special character in the mapping name will disable sort/filter/group functionality? Even if we delimit it with [ & ]?

Thanks,
Som


GT Gnanasownthari Thirugnanam Syncfusion Team March 8, 2018 12:49 PM UTC

Hi Som, 

Please ignore our previous Grouping/Sorting/Filtering related query. 

You can display the value in Display and perform the Filtering/Sorting/Grouping operation if MappingName of column having special character by using Display and value binding like below code example. 

void AssociatedObject_AutoGeneratingColumn(object sender, AutoGeneratingColumnArgs e) 
{  
    //You can dispaly the value and perform the sorting filtering opertaion if MappingName having special character. 
    if (e.Column.MappingName == "Country/ShipCity") 
    {              
        e.Column.DisplayBinding = new Binding("[Country/ShipCity]"); 
        e.Column.ValueBinding = new Binding("[Country/ShipCity]"); 
    } 
} 

We have modified the sample based on your requirement, you can download it from below mentioned location. 


Regards, 
Gnanasownthari T. 

 



SC Somanna Chottekalapanda March 8, 2018 01:23 PM UTC

Thanks Gnanasownthari.


JG Jai Ganesh S Syncfusion Team March 9, 2018 04:14 AM UTC

Hi Som,  
 
Thank you for the update. 
 
Please let us know if you need further assistance on this. 
 
Regards, 
Jai Ganesh S


DH Danial Hughes August 12, 2019 11:59 AM UTC

Is it possible to do this in an attached behavior rather than code behind?

Thanks
Danial


FP Farjana Parveen Ayubb Syncfusion Team August 13, 2019 09:39 AM UTC

Hi Danial, 
 
Thank you for using Syncfusion controls. 
 
In our previous update, we have used the AutoGenerateColumn event and set the DisplayBinding and ValueBinding for a column. But we can able to set the DisplayBinding and ValueBinding in Xaml, but you to disable the AutoGenerateColumn property and defines the columns manually in SfDataGrid.  
 
Please refer the following code snippet, 
 
<syncfusion:SfDataGrid x:Name="sfGrid" 
                               AllowFiltering="True" 
                               AllowGrouping="True" 
                               ShowGroupDropArea="True" 
                               ItemsSource="{Binding DataTableCollection}"                              
                               AutoGenerateColumns="False" 
                              > 
        <syncfusion:SfDataGrid.Columns> 
            <syncfusion:GridTextColumn MappingName="Id"/> 
            <syncfusion:GridTextColumn MappingName="Name"/> 
            <syncfusion:GridTextColumn MappingName="Country/ShipCity" DisplayBinding="{Binding [Country/ShipCity]}" ValueBinding="{Binding [Country/ShipCity] }"/> 
        </syncfusion:SfDataGrid.Columns> 
         
        </syncfusion:SfDataGrid> 
 
 
Please revert us if we misunderstood anything and provide more details about your requirement. It will be helpful for us to provide you with solution at earliest. 
 
Regards, 
Farjana Parveen A 


Loader.
Up arrow icon