We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Bind DataTable to SfDataGrid and use DataTable Column Captions as Column Names in the SfDataGrid

I wish to overwrite the HeaderText for the SfDataGrid Columns using DataTable Column captions. However, the SfDataGrid Column names do not get updated immediately. How can I get the Column names to update immediately in the UI?

In my code below, I bind the SfDataGrid BlockOutputDataGrid to a DataTable testDt.

Here is my code:

this.BlockOutputDataGrid.ItemsSource = testDt;
this.BlockOutputDataGrid.UpdateLayout();
foreach (var x in this.BlockOutputDataGrid.Columns)
{
                x.HeaderText = ((DataTable) this.BlockOutputDataGrid.ItemsSource).Columns[x.HeaderText].Caption;
}
this.BlockOutputDataGrid.UpdateLayout();

5 Replies

SV Srinivasan Vasu Syncfusion Team July 20, 2017 11:05 AM UTC

Hi Keenan, 

 
Thanks for contacting Syncfusion support. 

 
We have checked your query and we have prepared a sample as per your requirement.  In sample, we have changed the HeaderText based on DataColumn.Caption in Button click event. It will be updated immediately in UI. By default, we are using DataColumn.ColumnName property as HeaderText of GridColumn in SfDataGrid.
 
Please refer the below code. 
 
 private void Button_Click(object sender, RoutedEventArgs e)
        {            
            foreach (var x in this.sfGrid.Columns)
            {
                x.HeaderText = ((DataTable)this.sfGrid.ItemsSource).Columns[x.HeaderText].Caption;
            }         
        }


 
 
Also, you can achieve the same while initial loading of SfDataGrid by using SfDataGrid.AutoGeneratingColumn event.
 

 
Please refer the below code. 

 
        this.sfGrid.AutoGeneratingColumn += SfGrid_AutoGeneratingColumn;                  
        
        private void SfGrid_AutoGeneratingColumn(object sender, AutoGeneratingColumnArgs e)
        {             
            e.Column.HeaderText = _viewManager.GridData.Columns[e.Column.HeaderText].Caption;
        }


 

 

 

 
Regards, 
Srinivasan 



LL llllllllll July 20, 2017 02:52 PM UTC

Thank you. The AutoGeneratingColumn event solution worked best for me.


this.BlockOutputDataGrid.AutoGeneratingColumn += dataGrid_AutoGeneratingColumn;

private void dataGrid_AutoGeneratingColumn(object sender, AutoGeneratingColumnArgs e)
        {
            // Column header renaming using caption
            e.Column.HeaderText = ((DataTable)this.BlockOutputDataGrid.ItemsSource).Columns[e.Column.HeaderText].Caption;
        }



SV Srinivasan Vasu Syncfusion Team July 20, 2017 03:03 PM UTC

Hi Keenan, 
  
Thanks for your update. 
  
Please let us know if you need further assitance. 
  
Regards, 
Srinivasan 
  
  



MD Mario Del Rosario June 15, 2020 03:33 AM UTC

Hi, related to DataTable working with Sfdatagrid, how do you incorporate column combo type without using IEnumerable but by dataTable both the data of the sfdatagrid and the combo inside the grid?


SS Susmitha Sundar Syncfusion Team June 15, 2020 01:48 PM UTC

Hi Mario, 
 
Thank you for using Syncfusion controls. 
 
We suspect that you want to bind the DataTable for both SfDataGrid and GridComboBoxColumn. We have prepared the sample to bind the different table for both. Please refer the below sample, 
 
 
 If we misunderstood your query, kindly brief your actual requirement, it would be helpful for us to provide the solution as soon as possible. 
 
Regards, 
Susmitha S 


Loader.
Live Chat Icon For mobile
Up arrow icon