Vishal
Vishal

Reputation: 6378

Get name of the item instead of parent Id in dataGrid

I have a table called Groups in my Database. Here is the structure of that table:

GroupID      |    int              |    not null, Primary Key  1------+
GroupName    |    nvarchar(100)    |    not null                      |
ParentID     |    int              |    null                   *------+

As you can see in the table above, the ParentID column is referencing GroupID. Thus its an Hierarchical Data structure.

Sample Data in above table:

GroupID    |    GroupName    |    ParentID
-----------+-----------------+-------------
    1      |        a        |     NULL
    2      |        b        |     1
    3      |        c        |     NULL
    4      |        d        |     2
    5      |        e        |     1
    6      |        f        |     5

Now, I have a DataGrid. Inside that DataGrid I have two columns.

First column is Group Name and Second Column is Parent Name

So, I have written the required XAML to get the expected output:

<DataGrid Grid.Row="5" Grid.Column="1" ItemsSource="{Binding Groups}" 
          SelectedItem="{Binding SelectedGroup}"
          AutoGenerateColumns="False" CanUserAddRows="False"
          SelectionMode="Single" SelectionUnit="FullRow">

    <DataGrid.Columns>

        <DataGridTemplateColumn Header="Group Name">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding GroupName}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding GroupName}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>

        <DataGridTemplateColumn Header="Parent Name">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding ParentID}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding ParentID}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>

    </DataGrid.Columns>

</DataGrid>

In the above code I get the output:

    Group Name    |    Parent Name
------------------+-----------------
        a         |     
        b         |        1
        c         |     
        d         |        2
        e         |        1
        f         |        5

But I want the output to be :

    Group Name    |    Parent Name
------------------+-----------------
        a         |     
        b         |        a
        c         |     
        d         |        b
        e         |        a
        f         |        e

I know I have used {Binding ParentID} inside the Second Column. But with what should I replace this binding to get expected output.

Upvotes: 1

Views: 758

Answers (1)

Dennis
Dennis

Reputation: 37780

Assuming, that you're using Group class like this:

public class Group
{
    public int GroupID { get; set; }
    public string GroupName { get; set; }
    public int ParentID { get; set; }
    public Group Parent { get; set; }
}

and that the data source for DataGrid is a collection of Group, the binding expression for the second column should look like this:

<DataGridTemplateColumn Header="Parent Name">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Parent.GroupName}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Parent.GroupName}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

Note, that you have to use either lazy loading, or explicitly load reference to parent group from query:

context
    .Groups
    .Include(_ => _.Parent)
    .ToList();

Upvotes: 2

Related Questions