Datagrid
In practical applications, it is often encountered to calculate the results of a new column based on other columns, there are two ways to achieve such a function: a direct use of SQL statements, and the other is to dynamically add the binding. The first method has been introduced before. Here is the implementation of the second method:
Adddatasetcolumn.aspx
<%@ Page language= "vb" autoeventwireup= "false" codebehind= "Adddatasetcolumn.<a href=" http:// www.chinaitpower.com/Dev/Web/Asp/index.html "target=" _blank ">asp</a>x.vb"
inherits= "<a href=" http://www.chinaitpower.com/Dev/Web/Asp/index.html target= "_blank" >asp</a> Xweb.adddatasetcolumn "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title>AddDataSetColumn</title>
<meta name= "generator" content= "Microsoft Visual Studio. NET 7.1" >
<meta name= "Code_language" content= "Visual Basic. NET 7.1" >
<meta name= "vs_defaultClientScript" content= "JavaScript" >
<meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" >
</HEAD>
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >asp</a>:D atagrid id= "DATAGRID1" runat= "Server" autogeneratecolumns= "False"
Showfooter= "True" width= "100%" >
Forecolor= "Navy" backcolor= "Lightsalmon" ></HeaderStyle>
<footerstyle backcolor= "#FFCCFF" ></FooterStyle>
<Columns>
<<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >ASP</A> TemplateColumn headertext= "Order Number" >
<ItemTemplate>
<<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >asp</a>:label id= " OrderID "runat=" Server "></<a href=" http://www.chinaitpower.com/Dev/Web/Asp/index.html "target=" _blank " >asp</a>:Label>
</ItemTemplate>
</<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >ASP</A> Templatecolumn>
<<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >ASP</A> TemplateColumn headertext= "Product No." >
<ItemTemplate>
<<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >asp</a>:label id= " ProductID "runat=" Server "></<a href=" http://www.chinaitpower.com/Dev/Web/Asp/index.html "target=" _blank " >asp</a>:Label>
</ItemTemplate>
</<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >ASP</A> Templatecolumn>
<<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >ASP</A> TemplateColumn headertext= "Unit Price" >
<ItemTemplate>
<<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >asp</a>:label id= " UnitPrice "runat=" Server "></<a href=" http://www.chinaitpower.com/Dev/Web/Asp/index.html "target=" _blank " >asp</a>:Label>
</ItemTemplate>
</<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >ASP</A> Templatecolumn>
<<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >ASP</A> TemplateColumn headertext= "Quantity" >
<ItemTemplate>
<<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >asp</a>:label id= " Quantity "runat=" Server "></<a href=" http://www.chinaitpower.com/Dev/Web/Asp/index.html "target=" _blank " >asp</a>:Label>
</ItemTemplate>
</<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >ASP</A> Templatecolumn>
<<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >ASP</A> TemplateColumn headertext= "Discount" >
<ItemTemplate>
<<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >asp</a>:label id= " Discount "runat=" Server "></<a href=" http://www.chinaitpower.com/Dev/Web/Asp/index.html "target=" _blank " >asp</a>:Label>
</ItemTemplate>
</<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >ASP</A> Templatecolumn>
<<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >ASP</A> TemplateColumn headertext= "Grand total" >
<ItemTemplate>
<<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >asp</a>:label id= " Lbltotal "runat=" Server "></<a href=" http://www.chinaitpower.com/Dev/Web/Asp/index.html "target=" _blank " >asp</a>:Label>
</ItemTemplate>
</<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >ASP</A> Templatecolumn>
</Columns>
</<a href= "http://www.chinaitpower.com/Dev/Web/Asp/index.html" target= "_blank" >asp</a>:D Atagrid >
</form>
</body>
</HTML>
AddDataSetColumn.aspx.vb
Imports System.Web.UI.WebControls
Public Class Adddatasetcolumn
Inherits System.Web.UI.Page
#Region the code generated by the Web Forms Designer
' This call is required by the Web Forms Designer.
<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()
End Sub
Protected WithEvents DataGrid1 as System.Web.UI.WebControls.DataGrid
' NOTE: The following placeholder declarations are required by the Web Forms Designer.
' Do not delete or move it.
Private Designerplaceholderdeclaration as System.Object
Private Sub Page_Init (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Mybase.init
' CodeGen: This method call is required by the Web Forms Designer
' Do not modify it using 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
Dim strcnn as String = "Data source=.\netsdk;" Initial Catalog=northwind; User Id=sa; password=; "
Dim OCn as New System.Data.SqlClient.SqlConnection (STRCNN)
Dim strSQL as System.String = "SELECT * from [Order Details]"
Dim ds as System.Data.DataSet = New System.Data.DataSet
Ocn.open ()
Dim da as System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter (strSQL, OCn)
' can be implemented directly using SQL statements, this method can be directly bound, do not need to use a template
' Cmd.commandtext = ' Select *,unitprice * Quantity * (1-discount) as total from [order Details] "
' We use another method to add a column to the dataset before the DataGrid is bound
Da. Fill (ds, "DS")
Dim Dvwgrid as DataView = ds. Tables (0). DefaultView
DataGrid1.DataSource = Dvwgrid
Datagrid1.databind ()
Ocn.close ()
Ocn.dispose ()
End Sub
Private Sub Datagrid1_itemdatabound (ByVal sender as Object, ByVal e as DataGridItemEventArgs) _
Handles Datagrid1.itemdatabound
Dim elemtype as ListItemType = E.item.itemtype
If (elemtype = listitemtype.item Or elemtype = listitemtype.alternatingitem) Then
' Convert DataItem back to DataRowView object
Dim Mydatarowview as DataRowView = CType (e.Item.DataItem, DataRowView)
Dim MyDataRow as DataRow = Mydatarowview.row
CType (e.item.cells (0). FindControl ("OrderID"), Label). Text = MyDataRow ("OrderID")
CType (E.item.cells (1). FindControl ("ProductID"), Label). Text = MyDataRow ("ProductID")
CType (E.item.cells (2). FindControl ("UnitPrice"), Label). Text = MyDataRow ("UnitPrice")
CType (E.item.cells (3). FindControl ("Quantity"), Label). Text = MyDataRow ("Quantity")
CType (E.item.cells (4). FindControl ("Discount"), Label). Text = MyDataRow ("Discount")
Dim Dblprice as Double = Double.Parse (MyDataRow ("UnitPrice"))
Dim intquantity as Integer = Int32.Parse (MyDataRow ("Quantity"))
Dim Intdiscount as Double = Double.Parse (MyDataRow ("Discount"))
Dim ntotal as String = String.Format ("{0:c}", Dblprice * (1-intdiscount) * intquantity)
CType (E.item.cells (5). FindControl ("Lbltotal"), Label). Text = Ntotal
End If
End Sub
End Class