Datagrid
In ASP.net, the DataGrid uses a relatively high frequency control, and in everyday applications, how do you use category headings in a DataGrid?
What is a category title? For example, when we use the DataGrid, we display relevant data in a certain order, for example, when using the Northwind database of MS SQL Server, in the Product table, each product is a category. These categories are defined in the category table. Then we can show what products each category has in the DataGrid in the order of the categories in the Category table.
From the above diagram, you can see that in this DataGrid, the Blue row heading is a category heading, which clearly indicates the category to which the product belongs, and below the category heading is all the items under the category.
OK, so much so, let's start with the design step-by-step. (The following assumes that you have a rudimentary grasp of the basic operations of vs.net, with preliminary knowledge of asp.net and vb.net).
First of all, we set up a project called subheading, choose VB.net. Then add a DataGrid and modify its HTML code as follows:
? Xml:namespace PREFIX = ASP/><asp:boundcolumn datafield= "UnitsInStock" headertext= "Stock level" ></asp: Boundcolumn>
The goal is to set a few bound columns in the DataGrid, and note that the AutoGenerateColumns property in the DataGrid is set to False. After we write the ItemDataBound event for a while, we continue to edit the code as follows:
Forecolor= "BLACK" backcolor= "White" cellpadding= "3"
Gridlines= "None" cellspacing= "1"
onitemdatabound= "Datagrid1_itemdatabound" >
Then we'll look at how to write the logical part of the code. In fact, the process of doing category headings is very simple, the process is:
Read the product table's data from the database with an SQL statement, and then place it in the dataset's default DataTable.
Then check the categories that each product belongs to, and if you find that the category of a product is different from the category in the previous record, then you can be sure that the current product belongs to a new category, and you can insert new rows and modify them to become category headings.
The following is the code for Page_Load ():
Private Sub Page_Load (ByVal sender as System.Object, Byvale as System.EventArgs)
Handles MyBase.Load
Dim ConnectionString as String
ConnectionString = "Server=localhost;database=northwind; Uid=sa "
Dim CommandText as String = "Select CategoryName, ProductName," & _
"Cast (UnitPrice as varchar) as UnitPrice, UnitsInStock" & _
"From Products" & _
"INNER JOIN Categories on" & _
"Products.CategoryID = Categories.CategoryID" & _
"ORDER by Products.CategoryID"
Dim MyConnection as New SqlConnection (ConnectionString)
Dim mycommand as New SqlDataAdapter (CommandText, MyConnection)
Dim DS as New DataSet ()
Mycommand.fill (DS)
Dim Curcat as String ' indicates the category to which the product belongs in the current record
Dim Prevcat as String ' indicates the category to which the product belongs in the previous record
Dim row as TableRow ' rows to insert category headings
Dim i as Integer = 0 ' where you want to insert the category header row, with I
' Traverse the result set to find the row to insert the category heading
Do While I <= DS. Tables (0). Rows.count-1
Curcat = ds. Tables (0). Rows (i). Item ("CategoryName")
If curcat <> Prevcat Then
' If you find that the categories of the two records are different
Prevcat = Curcat
Dim Shrow as DataRow = ds. Tables (0). NewRow
Shrow ("ProductName") = ds. Tables (0). Rows (i). Item (0)
' Modify the row title to the category heading name
Shrow ("UnitPrice") = "subhead" To set a temporary token
Ds. Tables (0). Rows.insertat (Shrow, i) ' Inserts a new category header row
i + 1
End If
i + 1
Loop
DataGrid1.DataSource = ds
Datagrid1.databind ()
End Sub
In the above Page_Load event, the first is to connect the product table and the category table with the SQL statement, resulting in a result set of all the products in each category in the Category table, then traversing the result set, and then checking the categories that each product belongs to. If you find that the category of a product is different from the category in the previous record, then you can be sure that the current product belongs to a new category, you can insert a new row as the category header row, and in the newly inserted row, we write a shrow ("unitprice") = " Subhead, "What is this for?"
This is just a temporary token, because the new inserted category header row is different from the normal row, we want to set its style, so in its Item_bound event, the code is as follows:
Private Sub datagrid1_itemdatabound (sender as Object, E as DataGridItemEventArgs)
Select Case E.item.itemtype
Case ListItemType.AlternatingItem, ListItemType.Item
' If it's found to be a category header row, format it
If e.item.cells (1). Text.equals ("subhead") Then
' Set column width
' Set the cell to a ColSpan of 3
E.item.cells (0). ColumnSpan = 3
' Fit into a new category header row, remove cells from the
E.item.cells.removeat (2)
E.item.cells.removeat (1)
E.item.cells (0). Attributes.Add ("Align", "left")
E.item.cells (0). Font.Bold = True
E.item.backcolor = Color.FromArgb (204,204,255)
End If
End Select
End Sub
Finally, don't forget the imports System.Data.SqlClient.
This procedure is passed under Win2000 Server+vs.net 2002, or it can be run under Vs.net 2003.