[DataGrid] How to bind a text to an header when AutoGenerateColumns=True?

1 Answer 205 Views
DataGrid
Sébastien
Top achievements
Rank 2
Iron
Iron
Iron
Sébastien asked on 27 Apr 2022, 05:07 PM | edited on 27 Apr 2022, 05:09 PM

Hi,

In order to same the header text for the first 3 columns, I use the event "PrepareCell" event in the StringNameColumn. The StringNameColumn is used for the first 3 columns.

My solution works (Expando.png+SringNameColumn.PrepareCell), but I'm sure there is a better way, because with my solution the header text will be set over and over for each row!

Is there a better way? In this documentation page, I saw: https://docs.telerik.com/devtools/universal-windows-platform/controls/raddatagrid/columns/datagrid-overview?msclkid=9d05cf25c64c11ecac794eb0f892e42a

  • Mixed - by adding columns to the RadDataGrid.Columns collection and also set RadDataGrid.AutoGenerateColumns="True"

but I didn't find any documentation on how to achieve this. Do you have an example of mixed mode?

Thank you

1 Answer, 1 is accepted

Sort by
0
Accepted
Lance | Manager Technical Support
Telerik team
answered on 27 Apr 2022, 05:28 PM

Hello Sebastien,

The UWP DataGrid doesn't have the AutoGeneratingColumn event that you might have seen with other DataGrid (like the WPF GridView).

Instead, with UWP/WinUI, you can use the CollectionChanged event of the DataGrid's Columns property to know when a column is added or removed from the collection.

MyDataGrid.Columns.CollectionChanged

Although the Columns property is of type DataGridColumnCollection, it is based off ObservableCollection. This means you also get the standard NotifyCollectionChangedEventArgs which contains any newly added column reference:

 

To your follow up question of what "mixed mode" is, this isn't any special mode or extra property to set. That simply refers to the scenario when you have AutoGenerateColumns=True and are manually adding columns.

For example, this is a mixed mode situation:

<!-- This is a mixed mode setup because there are manually added column, but the AutoGenerateColumns property is set to true -->
<telerikGrid:RadDataGrid AutoGenerateColumns="True">
    <telerikGrid:RadDataGrid.Columns>
        <telerikGrid:DataGridTextColumn Header="Country"/>
    </telerikGrid:RadDataGrid.Columns>
</telerikGrid:RadDataGrid>

 

Note: AutoGenerateColumns only works for the known properties when the ItemsSource is set. It is not going to constantly look for new properties added to an object (like in the case of an Expando).

Regards,
Lance | Manager Technical Support
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
DataGrid
Asked by
Sébastien
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Lance | Manager Technical Support
Telerik team
Share this question
or