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.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Thread ID:

Created:

Updated:

Platform:

Replies:

131618 Jul 19,2017 11:59 PM UTC Jul 20,2017 03:03 PM UTC WPF 3
loading
Tags: SfDataGrid
llllllllll
Asked On July 19, 2017 11:59 PM UTC

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();

Srinivasan Vasu [Syncfusion]
Replied On 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 


llllllllll
Replied On 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;
        }


Srinivasan Vasu [Syncfusion]
Replied On July 20, 2017 03:03 PM UTC

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


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;