Dim CartView As DataView
Dim runningTotal As Double = 0
'Cart 是 Page 上的一个属性
'ReadOnly Property Cart As DataTable
' Get
' Dim tmpCart As DataTable
' Dim i As Integer
' Dim dr As DataRow
'
' If Session("DG_ShoppingCart") Is Nothing Then
' tmpCart = new DataTable()
' tmpCart.Columns.Add(new DataColumn("数量", GetType(String)))
' tmpCart.Columns.Add(new DataColumn("产品", GetType(String)))
' tmpCart.Columns.Add(new DataColumn("价格", GetType(Double)))
' tmpCart.Columns.Add(new DataColumn("礼品包", GetType(Boolean)))
' Session("DG_ShoppingCart") = tmpCart
' 第一次加载 -- 预填充一些数据
' For i= 1 to 6
' dr = tmpCart.NewRow()
' dr(0) = "1"
' dr(1) = "产品" & i.ToString
' dr(2) = 1.23 * (i+1)
' dr(3) = false
' tmpCart.Rows.Add(dr)
' Next
' Return tmpCart
' Else
' Return Session("DG_ShoppingCart")
' End If
'End Get
'End Property
'Sub Page_Init(sender As Object, e As EventArgs)
' MyDataGrid.EnableViewState = true
'End Sub
Sub Page_Load(sender As Object, e As EventArgs)
'CartView = Cart.DefaultView
If Not IsPostBack Then
BindGrid
End If
End Sub
'Sub BindGrid()
' MyDataGrid.DataSource = CartView
' MyDataGrid.DataBind()
' End Sub
Sub BindGrid()
Dim objconnection1 As SqlConnection '定义连接
Dim strConnection1 As String = "user id=sa;password=1979;database=Northwind;server=192.168.0.253;connect Timeout=30"
objconnection1 = New SqlConnection(strConnection1) '引用
Dim strsql As String = "select * from customers"
Dim objDataSet As New DataSet()
Dim objadapter As New SqlDataAdapter(strsql, objconnection1)
objadapter.Fill(objDataSet, "testtable")
Dim objtable As DataTable
objtable = objDataSet.Tables("testtable")
Dim objDataView As New DataView(objDataSet.Tables("testtable"))
MyDataGrid.DataSource = objDataView
MyDataGrid.DataBind()
End Sub
Sub btnUpdate_click(sender As Object, e As EventArgs)
Dim i As Integer
Dim _item As DataGridItem
Dim dr As DataRow
For i = 0 To MyDataGrid.Items.Count - 1
_item = MyDataGrid.Items(i)
Dim qtyTextBox As TextBox= _item.FindControl("txtQty")
Dim giftCheckBox As CheckBox = _item.FindControl("chkGIft")
' 对于数据库,我们应使用一条更新命令。
' 因为这是一个内存内数据表,所以我们只更改内存内的行。
' dr = Cart.Rows(i)
' dr(0) = qtyTextBox.Text
' dr(3) = giftCheckBox.Checked
Next
BindGrid
End Sub
' Function CalcTotal (count As Integer, price As Double) As Double
' Dim total As Double
'
' total = count * price
' runningTotal += total
'
' CalcTotal = total
' End Function
Dim CartView As DataView
Dim runningTotal As Double = 0
'Cart 是 Page 上的一个属性
ReadOnly Property Cart As DataTable
Get
Dim tmpCart As DataTable
Dim i As Integer
Dim dr As DataRow
If Session("DG_ShoppingCart") Is Nothing Then
tmpCart = new DataTable()
tmpCart.Columns.Add(new DataColumn("数量", GetType(String)))
tmpCart.Columns.Add(new DataColumn("产品", GetType(String)))
tmpCart.Columns.Add(new DataColumn("价格", GetType(Double)))
tmpCart.Columns.Add(new DataColumn("礼品包", GetType(Boolean)))
Session("DG_ShoppingCart") = tmpCart
' 第一次加载 -- 预填充一些数据
For i= 1 to 6
dr = tmpCart.NewRow()
dr(0) = "1"
dr(1) = "产品" & i.ToString
dr(2) = 1.23 * (i+1)
dr(3) = false
tmpCart.Rows.Add(dr)
Next
Return tmpCart
Else
Return Session("DG_ShoppingCart")
End If
End Get
End Property
'Sub Page_Init(sender As Object, e As EventArgs)
' MyDataGrid.EnableViewState = true
'End Sub
Sub Page_Load(sender As Object, e As EventArgs)
CartView = Cart.DefaultView
If Not IsPostBack Then
BindGrid
End If
End Sub
Sub BindGrid()
MyDataGrid.DataSource = CartView
MyDataGrid.DataBind()
End Sub
Sub btnUpdate_click(sender As Object, e As EventArgs)
Dim i As Integer
Dim _item As DataGridItem
Dim dr As DataRow
For i = 0 To MyDataGrid.Items.Count - 1
_item = MyDataGrid.Items(i)
Dim qtyTextBox As TextBox= _item.FindControl("txtQty")
Dim giftCheckBox As CheckBox = _item.FindControl("chkGIft")
' 对于数据库,我们应使用一条更新命令。
' 因为这是一个内存内数据表,所以我们只更改内存内的行。
dr = Cart.Rows(i)
dr(0) = qtyTextBox.Text
dr(3) = giftCheckBox.Checked
Next
BindGrid
End Sub
Function CalcTotal (count As Integer, price As Double) As Double
Dim total As Double
void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e)
{
// For bound columns, the edited value is stored in a TextBox.
// The TextBox is the 0th element in the column's cell.
TextBox qtyText = (TextBox)e.Item.Cells[3].Controls[0];
TextBox priceText = (TextBox)e.Item.Cells[4].Controls[0];
// With a database, use an update command to update the data. Because
// the data source in this example is an in-memory DataTable, delete the
// old row and replace it with a new one.
// Remove old entry.
CartView.RowFilter = "Item='" + item + "'";
if (CartView.Count > 0)
CartView.Delete(0);
CartView.RowFilter = "";
// Add new entry.
dr = Cart.NewRow();
dr[0] = qty;
dr[1] = item;
dr[2] = price;
Cart.Rows.Add(dr);