Sebastian
Sebastian

Reputation: 315

WPF Datagrid grouping with Datatable as ItemSource

I have a problem with grouping the data inside a datagrid. The data is contained inside a Datatable which is filled from a SQL Database and binded to my datagrid. This is all working fine...

Now I want to group my datagrid by the field 'ID' which contains the same number multiple times... I already tried a lot of possible solutions which I found here and there, but none of them are working. Most of them uses CollectionViewSource instead of a Datatable...

Isn't there any direct way to group (+filter) my datatable and show it in a datagrid? I hope anyone can help me to find an easy way or at least give me a hint what I need to look for.

Edit: To get the Data from the SQL Database I use the following (simplified) code:

this.db = new DBConnection(...);
DataTable dt_Applications = 
                        this.db.ExecuteReader( Properties.SqlQueries.SQL_GetApplications );

WPF:

<DataGrid Name="dg_Applications" 
          ItemsSource="{Binding dt_Applications}" 
          IsReadOnly="True" 
          CanUserResizeRows="False" 
          ClipboardCopyMode="IncludeHeader" 
          AutoGenerateColumns="True" />

The Data will look like:

[ID]   [Name]     [Var1]         [Var2]
0      "App0"     "Value0.1.0"   "Value0.2.0"
0      "App0"     "Value0.1.1"   "Value0.2.1"
0      "App0"     "Value0.1.2"   "Value0.2.2"
1      "App1"     "Value1.1.0"   "Value1.2.0"
2      "App2"     "Value2.1.0"   "Value2.2.0"
2      "App2"     "Value2.1.1"   "Value2.2.1"

Output:

[ID]   [Name]     [Var1]         [Var2]
"App0 [0]:"
-      -          "Value0.1.0"   "Value0.2.0"
-      -          "Value0.1.1"   "Value0.2.1"
-      -          "Value0.1.2"   "Value0.2.2"
"App1 [1]:"
-      -          "Value1.1.0"   "Value1.2.0"
"App2 [2]:"
-      -          "Value2.1.0"   "Value2.2.0"
-      -          "Value2.1.1"   "Value2.2.1"

Upvotes: 2

Views: 5251

Answers (2)

Joee
Joee

Reputation: 2016

To do the above thing programmatically you can try below code,

CollectionViewSource mycollection = new CollectionViewSource();
mycollection.Source = MyDataTable;
mycollection.GroupDescriptions.Add(new PropertyGroupDescription("PropertyToGroup"));
dataGrid.ItemsSource = mycollection.View;

Here MyDataTable is the datatable source for your grid and PropertyToGroup is the property you want to group by it is probably the column in the DataTable.

Upvotes: 1

Sebastian
Sebastian

Reputation: 315

I found a solution for my problem...

It is possible to bind the DataTable directly to a CollectionViewSource Object!

With this, there is nothing more to do. The DataGrid gets automatically reloaded when the DataTable was changed. All grouping and filtering can be done with it like described in the link from Alex Bell.

<Window.Resources>
    <CollectionViewSource x:Key="cvsDataTable" Source="{Binding dt_myDataTable}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="ID"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</Window.Resources>

<DataGrid Name="dg_myDataTable" ItemsSource="{Binding Source={StaticResource cvsDataTable}}">
    <DataGrid.GroupStyle>...</DataGrid.GroupStyle>
    <DataGrid.Columns>...</DataGrid.Columns>
</DataGrid>

Upvotes: 8

Related Questions