Gridview nested gridview

Source: Internet
Author: User
The nested gridview displays all child records at the same time for the selected parent record organization. For example, you can use it to create a complete list of products organized by category. The next example shows how to display a complete group product list in a single grid, as shown in:

The basic technique used is to create a gridview for the appendix, and each row of it is embedded with a gridview. These child gridview uses templatefield to insert it into the parent gridview. The only thing to note is that the child gridview cannot be bound when the parent gridview is bound, because the parent row is not created yet. Instead, wait for the databound time of the parent gridview to occur.
In this example, the parent gridview defines two columns, both of which are of the templatefield type. The first column combines the category name and type.

< ASP: templatefield Headertext = "Category" >

< Itemstyle Verticalalign = "TOP" Width = "20%" > </ Itemstyle >
< Itemtemplate >
< BR >
< B >
<% # Eval ( " Categoryname " ) %>
</ B >
< BR >
< BR >
<% # Eval ( " Description " ) %>
< BR >
</ Itemtemplate >
</ ASP: templatefield >

The second column contains the embedded product's gridview, which has two binding columns. The following is a brief description after the style features are omitted.Code:

< ASP: templatefield Headertext = "Products" >
< Itemstyle Verticalalign = "TOP" > </ Itemstyle >
< Itemtemplate >
< ASP: gridview ID = "Gridchild" Runat = "Server" Font-size = "XX-small" Backcolor = "White" Autogeneratecolumns = "False"
Cellpadding = "4" Bordercolor = "# Cc9966" Borderwidth = "1px" Width = "100%" Borderstyle = "NONE" >

< Rowstyle Forecolor = "#330099" Backcolor = "White" > </ Rowstyle >
< Headerstyle Font-bold = "True" Forecolor = "# Ffffcc" Backcolor = "#990000" > </ Headerstyle >

< Columns >
< ASP: boundfield Datafield = "Productname" Headertext = "Product name" >
< Itemstyle Width = "250px"   />
</ ASP: boundfield >
< ASP: boundfield Datafield = "Unitprice" Headertext = "Unit price" Dataformatstring = "{0: c }"   />
</ Columns >

</ ASP: gridview >
</ Itemtemplate >
</ ASP: templatefield >

Note that the tag of the second gridview does not set the performanceid attribute. This is because when the parent mesh is bound to its data source, the data sources of these grids are provided through programming.
Create two data sources, one for obtaining the product category and the other for obtaining the product of a specific category. The first query fills the parent gridview:

  < ASP: sqldatasource ID = "Sourcecategories" Runat = "Server" Connectionstring = "<% $ Connectionstrings: northwindconnectionstring %>"
Providername = "System. Data. sqlclient" Selectcommand = "Select * from categories" > </ ASP: sqldatasource >

 
You can bind the first mesh directly to the Data source:

< ASP: gridview ID = "Gridmaster" Runat = "Server" Gridlines = "NONE" Borderwidth = "1px" Bordercolor = "Tan"
Font-names = "Verdana" Cellpadding = "2" Autogeneratecolumns = "False" Backcolor = "Lightgoldenrodyellow"
Forecolor = "Black" Datakeynames = "Categoryid" Font-size = "X-small" Performanceid = "Sourcecategories" Onrowdatabound = "Gridmaster_rowdatabound" >

This code is very typical, and the secret is how to bind a child gridview. The second data source contains multiple calls to fill in the query of the gridview. Each time it gets products of different categories. Categoryid is provided as a parameter:

< ASP: sqldatasource ID = "Sourceproducts" Runat = "Server" Connectionstring = "<% $ Connectionstrings: northwindconnectionstring %>"
Providername = "System. Data. sqlclient" Selectcommand = "Select * from products where categoryid = @ categoryid" >
< Selectparameters >
< ASP: Parameter Name = "Categoryid" Type = "Int32"   />
</ Selectparameters >
</ ASP: sqldatasource >

To bind a child gridview, the corresponding gridview. rowdatabound event must be generated every time the row is generated and bound to the parent gridview. In this case, you can obtain the child gridview from the second column. You can call the select () method of the data source programmatically to bind it to the product information. To ensure that only products of the current category are displayed, you must obtain the categoryid field of the current item and pass it as a parameter. The Code is as follows:
Protected   Void Gridmaster_rowdatabound ( Object Sender, gridviewroweventargs E)
{
// Look for gridview items.
If (E. Row. rowtype = Datacontrolrowtype. datarow)
{
// Retrieve the gridview control in the second column.
Gridview gridchild = (Gridview) E. Row. cells [ 1 ]. Controls [ 1 ];

Sourceproducts. selectparameters [ 0 ]. Defaultvalue = Gridmaster. datakeys [E. Row. dataitemindex]. value. tostring ();
Object Data = Sourceproducts. Select (datasourceselectarguments. Empty );

// Bind the grid.
Gridchild. datasource = Data;
Gridchild. databind ();

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.