Presenting Parent/Child Data In A Data Grid Row

 
 
The Data Grid provides a richer UI presentation for Web applications. Most of the time, there may be a requirement to show one or more child data items that belong to the parent row in a column within the Data Grid. The following article gives an example of defining a Repeater control inside the Template column. The Repeater binds to the child data of the parent row.
 
 
  1. <asp:datagrid id="dataGrid1"

  2.               runat="server"

  3.               width="792px"

  4.               autogeneratecolumns="False"

  5.               cellpadding="0"

  6.               pagerstyle-mode="NumericPages"

  7.               pagerstyle-pagebuttoncount="20"

  8.               pagesize="20"

  9.               allowpaging="True"

  10.               allowsorting="True"

  11.  

  12. <Columns>

  13.  

  14. <!-- First Column With ID as hidden column -->

  15.  

  16. <asp:BoundColumn DataField="ID" Visible="False" HeaderText="ID">

  17.    <HeaderStyle Width="190px" HorizontalAlign="Center" >

  18.    </HeaderStyle>

  19. </asp:BoundColumn>

  20.  

  21. <!-- Second Column defined as Check Box in a Template Column -->

  22.  

  23. <asp:TemplateColumn HeaderText="Select" HeaderStyle-Width="40px" >

  24.    <ItemStyle Width="40px"></ItemStyle>

  25.    <ItemTemplate>

  26.       <input type="checkbox" runat="server" id="chkSelected"

  27.          checked ='<% # DataBinder.Eval(Container.DataItem,

  28.                                         "Selected") %>' />

  29.    </ItemTemplate>

  30. </asp:TemplateColumn>

  31.  

  32. <!-- Third Column bound to the Field Description -->

  33.  

  34. <asp:BoundColumn DataField="Description"

  35.                  SortExpression="Description"

  36.                  HeaderText="Description">

  37.    <HeaderStyle HorizontalAlign="Center"></HeaderStyle>

  38.    <ItemStyle Wrap="True" Width="2000px"></ItemStyle>

  39. </asp:BoundColumn>

  40.  

  41. <!-- Fourth Column a Numeric column for displaying Currency

  42.      with DataFormat -->

  43.  

  44. <asp:BoundColumn DataField="Amount" SortExpression="Amount"

  45.                  ReadOnly="True"

  46.                  HeaderText="Amount"

  47.                  DataFormatString="{0:$#,##0.0000;($#,##0.0000)}">

  48.    <HeaderStyle HorizontalAlign="Center"

  49.                 Width="70px"></HeaderStyle>

  50.    <ItemStyle HorizontalAlign="Right" Wrap="False"></ItemStyle>

  51. </asp:BoundColumn>

  52.  

  53. <!-- Fifth Column That displays the child record of the

  54.      row inside a Repeater control -->

  55.  

  56. <asp:TemplateColumn HeaderText="Child Data"

  57.                     HeaderStyle-HorizontalAlign="Center">

  58.    <ItemStyle HorizontalAlign="Left" Wrap="True"></ItemStyle>

  59.    <ItemTemplate>

  60.       <asp:Repeater ID="rptChild" runat = "server"

  61.            DataSource = '<%# ((System.Data.DataRowView)

  62.                         Container.DataItem).Row.

  63.                         GetChildRows("relationParentChild") %>'>

  64.             <ItemTemplate>

  65.                <asp:LinkButton ID="linkChild" Runat="server"

  66.                     CommandArgument=<%# DataBinder.

  67.                        Eval(Container.DataItem, "["ChildID"]")%>

  68.                     OnClick="linkChild_Click">

  69.                <%# DataBinder.Eval(Container.DataItem,

  70.                                    "["ChildFieldNameToDisplay"]")%>

  71.                </asp:LinkButton>

  72.             </ItemTemplate>

  73.       </asp:Repeater>

  74.    </ItemTemplate>

  75. </asp:TemplateColumn>

  76.  

  77. </Columns>

  78. <PagerStyle PageButtonCount="20" Mode="NumericPages"></PagerStyle>

  79. </asp:datagrid>

  80.  

  81. private void Page_Load(object sender, System.EventArgs e)

  82. {

  83.    System.Data.DataSet ds = Get Data Set();

  84.       // This Data Set should be populated with two

  85.       // tables one for the Parent Row Data and the

  86.       // second for the child data to be displayed

  87.       // in the repeater control

  88.       ds.Relations.Add("relationParentChild",

  89.                        ds.Tables[0].Columns["ID"],

  90.                        ds.Tables[1].Columns["FKID"]);

  91.    DataView dv = ds.Tables[0].DefaultView;

  92.    dataGrid1.DataSource = dv;

  93. }

  94.  
 
Copy to Clipboard      Download code as Text Format
 
  Date entered : 10th Jul 2007
  Rating : No Rating
  Accessed  :  8905
  Submitted by :  ahmedraza
 
   Add Comment  Printer friendly
   View All Comments  Email to a friend
   Add to my Favourites Download PDF version
   Rating  
 
                 Click each image to add
this page to each site.
 
 
 
Comments Available

    No comment available at the moment.Be the first one to make a comment

 
Related C# Source Codes
  • Spring
  • Wraps a string to a given number of characters using a string break character
  • Uppercase the first character of each word in a string
  • Make a string's first character uppercase
  • Strip whitespace (or other characters) from the beginning and end of a string

  • Copyright © 2013 VisualBuilder. All rights reserved