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

Columns header

I'm trying to use your SfDataGrid control.
My scenario is a textbox where the user inserts a value the SfDataGrid in binding with a Datatable.
<Syncfusion:SfDataGrid x:Name= "Data" AutoGenerateColumns="True"  ItemsSource="{Binding BomDelseDataTable}"  />
In the guide it is written that the control supports binding with the datatable, but when I modify the header, no changes appends.

When the textbox loses focus, I would like to update the SfDataGrid control with the new value of the colum header.
BomDelsDataTable.Columns.Item(OldNameColHeader).ColumnName = NewNameColHeader
I need to do this in Runtime after I've created it.

I use VB.net, WPF and MVVM.

3 Replies

BR Balamurugan Rajaraman Syncfusion Team August 22, 2017 01:41 PM UTC

HI Gianfranco Vrech, 
Thanks for contacting Syncfusion support. 
We have checked your query “How to change the header name at runtime”. You can able to change the column header at runtime by using HeaderText property of the GridColumn as like the below provided code snippet. 
Public Sub AddNewRecord(obj As Object) 
    TryCast(DirectCast(obj, Object())(1), SfDataGrid).Columns(0).HeaderText = DirectCast(DirectCast(obj, Object())(0), System.Windows.Controls.TextBox).Text 
End Sub 
Another way using the ColumnName instead of index 
Me.datagrid.Columns("EmployeeID").HeaderText = "NewColumnName" 
We have created the sample based upon your requirement and attached that sample for your reference you can able to get it from the below link 
Balamurugan R 

GV Gianfranco Vrech August 23, 2017 08:40 AM UTC

Ok, I tryed your solution and works fine, but in my scenario, I need to work with the datatable and not with the control.

So there's a way to force the new datatable content to view? I've more than one property to set...

SV Srinivasan Vasu Syncfusion Team August 24, 2017 03:01 PM UTC

Hi Gianfranco Vrech, 
We have checked your query and we have prepared a sample as per your requirement. While changing the DataColumn.ColumnName in DataTable, it will not be reflected in view, because SfDataGrid could not get any notification from DataTable Column property changed since DataTable doesn’t support INotifyPropertyChanged.  
So, we may have to re-create all column when changing the ColumnName. Instead of which, we can remove the old column and add new column based on ColumnName of DataColumn. 
Please refer the below example. 
  Public Sub AddNewRecord(obj As Object) 
        ' (((object[])obj)[1] as SfDataGrid).Columns[0].HeaderText = ((System.Windows.Controls.TextBox)((object[])obj)[0]).Text; 
        Dim previousColumnName As String = TryCast(TryCast(DirectCast(obj, Object())(1), SfDataGrid).DataContext, DataTableClass).EmployeeDetails.Columns(2).ColumnName 
        ' Change ColumnName of Name column in DataTableCollection 
        TryCast(TryCast(DirectCast(obj, Object())(1), SfDataGrid).DataContext, DataTableClass).EmployeeDetails.Columns(2).ColumnName = DirectCast(DirectCast(obj, Object())(0), System.Windows.Controls.TextBox).Text 
        Dim mappingName As String = TryCast(TryCast(DirectCast(obj, Object())(1), SfDataGrid).DataContext, DataTableClass).EmployeeDetails.Columns(2).ColumnName 
        Dim column = TryCast(DirectCast(obj, Object())(1), SfDataGrid).Columns.FirstOrDefault(Function(x) x.MappingName = previousColumnName) 
        ' Remove column based on MappingName from GridColumns 
        TryCast(DirectCast(obj, Object())(1), SfDataGrid).Columns.Remove(TryCast(column, GridColumn)) 
        ' Insert a column based on MappingName with GridColumns 
        TryCast(DirectCast(obj, Object())(1), SfDataGrid).Columns.Insert(2, New GridTextColumn() With { 
        .MappingName = mappingName 
    End Sub 
Also, please ensure that we have understood your query and your requirement is to change ColumnName? If your requirement is to change the caption or may be different from what we have understood, please revert us with more details. 

Live Chat Icon For mobile
Up arrow icon