如何添加、修改及删除UltraWebGrid?
经过几天奋战,终于搞定UltraWebGrid的添加、修改、删除,为避免各位仁兄少走弯路,特公告如下,请各位高手圈点:
Imports System.Data
Imports System.Data.SqlClient Imports Infragistics.Shared Imports Infragistics.WebUI Public Class temp2 Inherits System.Web.UI.PagePrivate Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Page.IsPostBack = False Then
Me.sdatemp.Fill(dstemp1)
Me.UltraWebGrid1.DataBind()End If
End Sub
Private Sub LoadUpdatableDataSet()
If dstemp1.t_file_name.Rows.Count <= 0 Then
' fill dataset with server data sdatemp.Fill(dstemp1) ' add guid column for new rows Dim colGUID As New DataColumn("GUID", GetType(String)) dstemp1.t_file_name.Columns.Add(colGUID) End IfEnd Sub
Private Sub UltraWebGrid1_InitializeLayout(ByVal sender As System.Object, ByVal e As Infragistics.WebUI.UltraWebGrid.LayoutEventArgs) Handles UltraWebGrid1.InitializeLayout
e.Layout.Bands(0).DataKeyField = "id"
e.Layout.AllowAddNewDefault _
= Infragistics.WebUI.UltraWebGrid.AllowAddNew.Yes e.Layout.AllowDeleteDefault _ = Infragistics.WebUI.UltraWebGrid.AllowDelete.Yes e.Layout.AllowUpdateDefault _ = Infragistics.WebUI.UltraWebGrid.AllowUpdate.Yes e.Layout.AddNewBox.Hidden = Falsee.Layout.CellClickActionDefault = _
Infragistics.WebUI.UltraWebGrid.CellClickAction.EditWith e.Layout.Bands(0)
.Columns.Add("GUID") .Columns.FromKey("GUID").Hidden = True End WithEnd Sub
Private Sub UltraWebGrid1_UpdateGrid(ByVal sender As Object, ByVal e As Infragistics.WebUI.UltraWebGrid.UpdateEventArgs) Handles UltraWebGrid1.UpdateGrid
Dim c_colNewCustomerDataRows As New Collection
LoadUpdatableDataSet()
' test to see if there new rows to add
If c_colNewCustomerDataRows.Count > 0 Then Dim drCustomers As dstemp.t_file_nameRow For Each drCustomers In c_colNewCustomerDataRows dstemp1.t_file_name.Rows.Add(drCustomers) Next End If' attempt to send updates to database
Try sdatemp.Update(dstemp1.t_file_name) Catch ex As Exception 'DisplayMessages(ex.Message.ToString) End Try' if new rows were added, then rebind
If c_colNewCustomerDataRows.Count > 0 Then UltraWebGrid1.DataBind() End IfEnd Sub
Private Sub UltraWebGrid1_UpdateRowBatch(ByVal sender As Object, ByVal e As Infragistics.WebUI.UltraWebGrid.RowEventArgs) Handles UltraWebGrid1.UpdateRowBatch
Dim dataKey As String = CStr(e.Row.DataKey)
Select Case e.Row.DataChanged
Case UltraWebGrid.DataChanged.Added
Dim newDataTableRow As DataRow = dstemp1.Tables(0).NewRow()
Try Dim c As UltraWebGrid.UltraGridCell For Each c In e.Row.Cells If -1 <> dstemp1.Tables(0).Columns.IndexOf(c.Column.Key) Then newDataTableRow(c.Column.Key) = c.Value End If Next c dstemp1.Tables(0).Rows.Add(newDataTableRow) Catch ex As Exception Me.UltraWebGrid1.ClientResponse.ResponseStatus = Infragistics.WebUI.UltraWebGrid.XmlHTTPResponseStatus.Fail Me.UltraWebGrid1.ClientResponse.StatusMessage = ex.Message Me.UltraWebGrid1.ClientResponse.Tag = ex.Message End Trye.Row.DataKey = e.Row.Cells.FromKey("id").Value
Case UltraWebGrid.DataChanged.Modified
If Nothing <> dataKey Then
LoadUpdatableDataSet()
Dim dr As DataRow = dstemp1.Tables(0).Rows.Find(New Object() {dataKey})
If Not Nothing Is dr ThenDim c As UltraWebGrid.UltraGridCell
For Each c In e.Row.Cells If -1 <> dstemp1.Tables(0).Columns.IndexOf(c.Column.Key) Then If Not dr(c.Column.Key).Equals(c.Value) Then dr(c.Column.Key) = c.Value End If End If Next c End If e.Row.DataKey = e.Row.Cells.FromKey("id").Value End IfEnd Select
End Sub
Private Sub UltraWebGrid1_DeleteRowBatch(ByVal sender As Object, ByVal e As Infragistics.WebUI.UltraWebGrid.RowEventArgs) Handles UltraWebGrid1.DeleteRowBatch
Dim dataKey As String = CStr(e.Row.DataKey)
LoadUpdatableDataSet()' in the case of deleted data, we used the DataKey value off of the row to locate the row in the datasource and remove it
If Nothing <> dataKey Then ' First we locate the row in the DataTable object with the corresponding key Dim dr As DataRow = dstemp1.Tables(0).Rows.Find(New Object() {dataKey}) ' if found, delete If Not Nothing Is dr Then dr.Delete() End If End If End SubEnd Class