关于Infragistics的UltraWebGrid的updatecellbatch事件

经过几天奋战,终于搞定UltraWebGrid的添加、修改、删除,为避免各位仁兄少走弯路,特公告如下,请各位高手圈点:

Imports System.Data
Imports System.Data.SqlClient
Imports Infragistics.Shared
Imports Infragistics.WebUI
Public Class temp2
    Inherits System.Web.UI.Page

     Private 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 If

    End 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 = False

        e.Layout.CellClickActionDefault = _
        Infragistics.WebUI.UltraWebGrid.CellClickAction.Edit

        With e.Layout.Bands(0)
            .Columns.Add("GUID")
            .Columns.FromKey("GUID").Hidden = True
        End With


    End 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 If

    End 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 Try

                e.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 Then

                        Dim 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 If

        End 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 Sub

 End Class
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值