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:
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:
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.