Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

I started a thread over on a MSDN forum about how to develop from scratch one of those "cool" looking 3rd party datagrids that have a nested grid for related data. At this point I'm just looking for help to get the data to show much less get into the fancy "tree like" selection column. The full thread can be reviewed here:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fd88c680-02ce-4e5d-9588-60cb380ddd85[]

My questions in short are #1. Is my XAML use of the row details template the correct path.

XML
<DataGrid IsReadOnly="False" AutoGenerateColumns="False" x:Name="dg_SmeAFe">
    <DataGrid.Columns>
        <DataGridCheckBoxColumn Header="AFE Select" IsReadOnly="False" />
        <DataGridTextColumn Binding="{Binding Path=AfeId}" Header="AFE ID" />
        <DataGridTextColumn Binding="{Binding Path=AfeStatusName}" Header="Afe Status" />
        <DataGridTextColumn Binding="{Binding Path=CustName}" Header="Customer Name" />
        <DataGridTextColumn Binding="{Binding Path=RegionName}" Header="Region Name" />
        <DataGridTextColumn Binding="{Binding Path=PropertyNumber}" Header="Property Number" />
    </DataGrid.Columns>

    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <DataGrid AutoGenerateColumns="False" x:Name="dgChld_SmeTasks" IsReadOnly="False" ItemsSource="{Binding}">
                <DataGridCheckBoxColumn Header="Task Select" IsReadOnly="False" />
                <DataGridTextColumn Binding="{Binding Path=DisplayName}" Header="Work to Perform" />
                <DataGridTextColumn Binding="{Binding Path=Status}" Header="Status" />
            </DataGrid>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>

</DataGrid>

and #2. how do I bind the data to the tables and tell them about their relationship?
The below code is pseudo code to show what I thought we be the approach.

C#
//Bind parent table(Afes) in Dataset(ds_AfeTaska) to Parent grid
AfeDg.ItemsSource = ds_AfeTaska.Tables["Afes"].DefaultView;

//Bind details table(APTasks) in Dataset(ds_AfeTaska) to details grid
AfeChilDg.ItemsSource = ds_AfeTaska.Tables["APTasks"].DefaultView;

//Inform parent there is a relation *Remember this is a bogus property*
AfeDg.IsRelationTable = true;

//Inform parent what column data provides the lookup key for the details grid row selection
AfeDg.RelationKeyColumn = "AfeID";


And yes I do already have a relationship defined in the DataSet between the two tables on the AfeID column. This seems so trivial yet not getting the answers from Google...although I learned some unrelated cool programming things.

Anyway any help would be greatly appreciated.
JB
Posted
Updated 19-May-16 20:23pm
v2

private void gItemInfo1_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
DataGridTextColumn Column;
int SelectedIndex;
Binding bItems;
try
{
if (((System.Windows.Controls.Panel)(e.DetailsElement)).Children.Count > 0)
{
SelectedIndex = ((DataGrid)(sender)).SelectedIndex;
DataGrid MyDetailsGrid = (DataGrid)e.DetailsElement.FindName("dgChld_SmeTasks");
MyDetailsGrid.Columns.Clear();

Column = new DataGridTextColumn();
Column.Header = "ID";
bItems = new Binding("IID");
bItems.Mode = BindingMode.TwoWay;
Column.Binding = bItems;
Column.Width = new DataGridLength(154, DataGridLengthUnitType.Star);

MyDetailsGrid.Columns.Add(Column);

Column = new DataGridTextColumn();
Column.Header = "Name";
bItems = new Binding("SItemName");
bItems.Mode = BindingMode.TwoWay;
Column.Binding = bItems;
Column.Width = new DataGridLength(155, DataGridLengthUnitType.Star);
MyDetailsGrid.Columns.Add(Column);

Column = new DataGridTextColumn();
Column.Header = "Price";
bItems = new Binding("IPrice");
bItems.Mode = BindingMode.TwoWay;
Column.Binding = bItems;
Column.Width = new DataGridLength(154, DataGridLengthUnitType.Star);
MyDetailsGrid.Columns.Add(Column);

Column = new DataGridTextColumn();
Column.Header = "Postal Code";
bItems = new Binding("IPostalCode");
bItems.Mode = BindingMode.TwoWay;
Column.Binding = bItems;
Column.Width = new DataGridLength(155, DataGridLengthUnitType.Star);
MyDetailsGrid.Columns.Add(Column);
MyDetailsGrid.Width = MyDetailsGrid.Width - 20;
List<itemsmapping> objItemMap = new List<itemsmapping> { (ItemsMapping)dg_SmeAFe.Items[SelectedIndex] };
MyDetailsGrid.ItemsSource = (List<itemsdetails>)objItemMap[0].ICollectionItemDetails;
}
}
catch (Exception)
{
// throw;
}
}

Some thing like this on "LoadingRowDetails"
 
Share this answer
 
Hey you must define the rows in the DetialsRowTemplate like this:

XML
<datagrid.rowdetailstemplate>
        <datatemplate>
            <datagrid autogeneratecolumns="False" x:name="dgChld_SmeTasks" isreadonly="False" itemssource="{Binding}" xmlns:x="#unknown">
              <datagrid.columns>
                <datagridcheckboxcolumn header="Task Select" isreadonly="False" />
                <datagridtextcolumn binding="{Binding Path=DisplayName}" header="Work to Perform" />
                <datagridtextcolumn binding="{Binding Path=Status}" header="Status" />
              </datagrid.columns>
            </datagrid>
        </datatemplate>
    </datagrid.rowdetailstemplate>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900