Use the asp.net datagrid to display the primary and secondary relationship data

Source: Internet
Author: User
Tags bind eval table name visual studio
asp.net|datagrid| Data | Do you see any actual work in the DataGrid to display data with primary and secondary structure? If you are familiar with ado.net and SQL Server2000, let's take a sample database, Northwind, and vb.net that takes advantage of vs.net to illustrate how to achieve this goal.
First, we'll first prepare the data with the primary and secondary structure to display.

The following are the contents of the VB.net code file HierarchicalDataGrid.aspx.vb to bind to hierarchicaldatagrid.aspx:

Imports System.Data.SqlClient

Public Class Hierarchicaldatagrid
Inherits System.Web.UI.Page
Protected WithEvents DataGrid1 as System.Web.UI.WebControls.DataGrid

#Region "Web Form Designer generated Code"

' The following call is necessary for the Web Form designer
<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()

End Sub

Private Sub Page_Init (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Mybase.init
' CodeGen: This method is called for the Web Form designer
' Don't change this here with the Code Editor.
InitializeComponent ()
End Sub

#End Region

Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load

' Open a database connection
Dim ordersconnection as New SqlConnection ("server= (local) \netsdk;database=northwind;" Trusted_connection=yes ")

' Get orders and order details
Dim Orderadapter as New SqlDataAdapter ("select * from Orders"; Select OrderID, Products.productname,[order Details]. Unitprice,[order Details]. Quantity,[order Details].discount from [Order details],products where [order Details]. Productid=products.productid ", ordersconnection)

' Create and populate the DataSet object
Dim Orderdataset as New DataSet ()

Orderadapter.fill (Orderdataset)

' Set the table name
Orderdataset.tables (0). TableName = "Orders"
Orderdataset.tables (1). TableName = "Order Details"

' Establish a parent-child relationship between two tables based on the OrderID field
Dim Parent as DataColumn = Orderdataset.tables ("Orders"). Columns ("OrderID")
Dim Child as DataColumn = Orderdataset.tables ("Order Details"). Columns ("OrderID")

Dim orderrelation as DataRelation = New DataRelation ("Orderrelation", Parent, Child, False)

' Add a relationship to the dataset
ORDERDATASET.RELATIONS.ADD (orderrelation)

' Bind the DataGrid to the Orders table
DataGrid1.DataSource = Orderdataset.tables ("Orders"). DefaultView
DataBind ()

End Sub

Private Sub Datagrid1_itemdatabound (ByVal sender as Object, ByVal e as System.Web.UI.WebControls.DataGridItemEventArgs) Handles Datagrid1.itemdatabound
E.item.cells (0). BackColor = System.Drawing.Color.Ivory
End Sub
End Class

In Hierarchicaldatagrid.aspx, we set up a template column for DATAGRID1 and embed another DataGrid in the template column, and the DataGrid at the parent level is used to display the master record, such as: order. The sub-level DataGrid is used to display detailed information, such as order details. This is how we dynamically specify the data source for the DataGrid:

Datasource= ' <%# Ctype (Container.dataitem,datarowview). CreateChildView ("orderrelation")%> ' >

The above sentence is the key to achieving primary and secondary relational data. Based on the current parent-level record, Datarowview.createchildview takes the name of the relationship created earlier as a parameter to obtain the child record and binds it to the child DataGrid.

Here's the code for the ASPX page we want to display, hierarchicaldatagrid.aspx:

<%@ Page language= "vb" autoeventwireup= "false" codebehind= "HierarchicalDataGrid.aspx.vb" inherits= " Hierarchicaldatagrid.hierarchicaldatagrid "%>
<%@ Import namespace= "System.Data"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title> shows examples of primary and secondary relationship data </title>
<meta name= "generator" content= Microsoft Visual Studio.NET 7.0 >
<meta name= "Code_language" content= "Visual Basic 7.0" >
<meta name= "vs_defaultClientScript" content= "JavaScript" >
<meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" >
</HEAD>
<body>
<form id= "Frmdatagrid" method= "POST" runat= "Server" >
<p align= "center" >
<asp:datagrid id= "DataGrid1" bordercolor= "#0099FF" runat= "Server" showheader= "False" width= "470px" cellpadding= "0 "cellspacing=" 0 "autogeneratecolumns=" False "borderwidth=" 2px ">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<table cellspacing= "0" cellpadding= "0" width= "100%" border= "0" >
<TR>
&LT;TD bgcolor= "#FF6600" ><B> Order Number:
<%# DataBinder.Eval (Container.DataItem, "OrderID")%>
</B>
</TD>
</TR>
<TR>
&LT;TD align= "Right" >
<asp:datagrid id=datagrid2 runat= "Server" autogeneratecolumns= "False" bordercolor= "#33FF33" datakeyfield= " OrderID "Datasource= ' <%# Ctype (Container.dataitem,datarowview). CreateChildView ("orderrelation")%> ' >
<Columns>
<asp:boundcolumn visible= "False" datafield= "OrderID" readonly= "True" ></asp:BoundColumn>
<asp:templatecolumn headertext= "Product Name" >
<ItemTemplate>
<%# DataBinder.Eval (Container.DataItem, "ProductName")%>
</ItemTemplate>
</asp:TemplateColumn>
<asp:boundcolumn datafield= "UnitPrice" headertext= "Unit Price" ></asp:BoundColumn>
<asp:boundcolumn datafield= "Quantity" headertext= "Quantity" ></asp:BoundColumn>
<asp:boundcolumn datafield= "Discount" headertext= "Discount" ></asp:BoundColumn>
</Columns>
</asp:DataGrid></TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></P>
</form>
</body>
</HTML>



Related Article

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.