Figure 2 The ASP DataGrid Attributes <asp:DataGrid
id="grdCustomer" name="grdCustomer"
runat="Server"
AllowSorting="True"
gridlines="None"
cellpadding="3"
AutoGenerateColumns="false"
headerstyle-backcolor="#333399"
headerstyle-font-names="tahoma"
headerstyle-font-size="8pt"
headerstyle-forecolor="#ffff66"
itemstyle-backcolor="White"
itemstyle-font-names="tahoma"
itemstyle-font-size="8pt"
alternatingitemstyle-backcolor="#ffdddd"
oneditcommand="OnEditCommand"
oncancelcommand="OnCancelCommand"
onupdatecommand="OnUpdateCommand"
ondeletecommand="OnDeleteCommand"
onpageindexchanged="OnPageChange"
onsortcommand="OnPageSort"
datakeyfield="CustomerID"
allowpaging="True"
pagesize="20"
>
<PagerStyle Mode="NextPrev"
Font-Name="tahoma"
Font-Size="8"
NextPageText="Next Page >>"
PrevPageText="<< Prev Page"
Position="Bottom">
</PagerStyle>
Figure 3 DataGrid Columns <columns>
<asp:ButtonColumn HeaderText="Delete" ButtonType="LinkButton"
Text="delete" CommandName="Delete"/>
<asp:editcommandcolumn edittext="edit" canceltext="cancel"
updatetext="update"/>
<asp:BoundColumn HeaderText="Customer ID" DataField="CustomerID"
readonly="true" visible="true" SortExpression="CustomerID"/>
<asp:templatecolumn headertext="Company Name"
SortExpression="CompanyName">
<ItemTemplate>
<asp:Label runat="server" ID="lblCompanyName"
text=<%#Container.DataItem("CompanyName")%>>
</asp:label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtCompanyName"
text=<%#Container.DataItem("CompanyName")%>>
</asp:textbox>
</EditItemTemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="Contact Name"
SortExpression="ContactName">
<ItemTemplate>
<asp:Label runat="server" ID="lblContactName"
text=<%#Container.DataItem("ContactName")%>>
</asp:label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtContactName"
text=<%#Container.DataItem("ContactName")%>>
</asp:textbox>
</EditItemTemplate>
</asp:templatecolumn>
<asp:BoundColumn HeaderText="City" DataField="City" readonly="false"
visible="true" SortExpression="City"/>
</columns>
Figure 5 The LoadData Method Private Sub LoadData()
'// Instantiate the Business Layer object
Dim oCustomer As Customer = New Customer()
'// Fill the DataGrid with the data from the DataSet
Dim sSort As String = viewstate("sortfield") & " " & _
viewstate("sortdirection")
Dim sTableName As String = "Customer"
'// Get the Customer data back in to a DataSet
Dim oDs As DataSet = oCustomer.GetData()
oDs.Tables(sTableName).DefaultView.Sort = sSort.Trim()
grdCustomer.Da taSource = oDs.Tables(sTableName).DefaultView
grdCustomer.DataBind()
'// Save the XML schema to Session state
Session.Add("XmlSchema", oDs.GetXmlSchema())
'// Destroy the objects
oDs.Dispose()
oCustomer = Nothing
End Sub
Figure 6 The Delete Event Public Sub OnDeleteCommand(ByVal sender As Object, _
ByVal e As DataGridCommandEventArgs)
Dim sTableName As String = "Customer"
'// Get the Customer data back into a DataSet
Dim oDs As DataSet = New DataSet()
Dim sXmlSchema As String = Session.Item("XmlSchema")
'// Read the XML Schema into the local DataSet.
Dim oSr As System.IO.StringReader = New _
System.IO.StringReader(sXmlSchema)
oDs.ReadXmlSchema(oSr)
'// Retrieve the row ID of the deleted row
Dim sCustomerID As String = e.Item.Cells(2).Text
'// Create a new datarow
Dim oRow As DataRow = oDs.Tables(sTableName).NewRow()
'// Put the deleted row's data into the new DataRow
oRow(Customer.bs_Customer.bs_Customer_CustomerID) = sCustomerID
'// Add the new DataRow to the new DataSet
oDs.Tables(sTableName).Rows.Add(oRow)
'// Accept the changes. This saves the record to the DataSet and
'// wipes away the delta.
oDs.AcceptChanges()
'// Make a final change to the row. This makes the DataSet think it
'// is a DELETE
oDs.Tables(sTableName).Rows(0).Delete()
'// Call the SaveData method of the business class
Dim oCustomer As Customer = New Customer()
oCustomer.SaveData(oDs)
'// Dispose of the objects
oDs.Dispose()
oCustomer = Nothing
'// Return to viewing mode.
grdCustomer.EditItemIndex = -1
'// Reload the data into the grid
LoadData()
End Sub
Figure 7 The Update Event Public Sub OnUpdateCommand(ByVal sender As Object, _
ByVal e As DataGridCommandEventArgs)
Dim sTableName As String = "Customer"
'// Get the Customer data back into a DataSet
Dim oDs As DataSet = New DataSet()
Dim sXmlSchema As String = Session.Item("XmlSchema")
'// Read the XML Schema into the local DataSet.
Dim oSr As System.IO.StringReader = New _
System.IO.StringReader(sXmlSchema)
oDs.ReadXmlSchema(oSr)
'// Retrieve the data the user updated
Dim sCustomerID As String = e.Item.Cells(2).Text
Dim sCompanyName As String = _
CType(e.Item.Cells(3).FindControl("txtCompanyName"), TextBox).Text
Dim sContactName As String = _
CType(e.Item.Cells(4).FindControl("txtContactName"), TextBox).Text
Dim sCity As String = CType(e.Item.Cells(5).Controls(0), _
TextBox).Text
'// Create a new DataRow
Dim oRow As DataRow = oDs.Tables(sTableName).NewRow()
'// Put the user's updated data into the new DataRow
oRow(Customer.bs_Customer.bs_Customer_CompanyName) = sCompanyName
oRow(Customer.bs_Customer.bs_Customer_CustomerID) = sCustomerID
oRow(Customer.bs_Customer.bs_Customer_ContactName) = sContactName
oRow(Customer.bs_Customer.bs_Customer_City) = sCity
'// Add the new DataRow to the new DataSet
oDs.Tables(sTableName).Rows.Add(oRow)
'// Accept the changes. This saves the record to the DataSet and
'// wipes away the delta.
oDs.AcceptChanges()
'// Make a final change to the row. This makes the DataSet think it
'// is an UPDATE
oDs.Tables(sTableName). _
Rows(0).Item(Customer.bs_Customer.bs_Customer_City) = sCity
'// Call the SaveData method of the business class
Dim oCustomer As Customer = New Customer()
oCustomer.SaveData(oDs)
'// Dispose of the objects
oDs.Dispose()
oCustomer = Nothing
'// Return to viewing mode.
grdCustomer.EditItemIndex = -1
'// Reload the data into the grid
LoadData()
End Sub
|