如何覆盖DevExpress GridView删除

时间:2010-07-15 17:27:47

标签: gridview devexpress gridcontrol

我有一个表(myTable),我想从中删除行但不删除它们。我使用myTable.ActiveFlag将它们过期。因此,当我从myTable“删除”一行时,我想运行UPDATE myTable SET ActiveFlag = 0 WHERE id = @rowId

使用带有Gridcontrol的DevExpress GridView执行此操作的最佳方法是什么?

我目前:

Private Sub gcMyTableMaintenance_EmbeddedNavigator_ButtonClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraEditors.NavigatorButtonClickEventArgs) Handles gcMyTableMaintenance.EmbeddedNavigator.ButtonClick
    If e.Button.ButtonType = DevExpress.XtraEditors.NavigatorButtonType.Remove Then
        //'TODO: Remove Row
    End If
    e.Handled = True
End Sub

我在考虑在这里运行存储过程或SQL语句,但是有更简单或更合适的方法吗?


以下是我最终代码的基本版本:

Private Sub gcMyTableMaintenance_EmbeddedNavigator_ButtonClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraEditors.NavigatorButtonClickEventArgs) Handles gcMyTableMaintenance.EmbeddedNavigator.ButtonClick
    If e.Button.ButtonType = DevExpress.XtraEditors.NavigatorButtonType.Remove Then
        Dim iMyTableId As Int32 = 0

        iMyTableId = gvMyTableMaintenance.GetFocusedRowCellValue("id")

        Using conn As New SqlConnection(MyConnectionString)
            Using lCmd As New SqlCommand()
                Try
                    lCmd.Connection = conn
                    lCmd.CommandType = CommandType.StoredProcedure
                    lCmd.CommandText = "ExpireFromMyTable"
                    lCmd.Parameters.AddWithValue("@myTableId", iMyTableId)
                    conn.Open()
                    lCmd.ExecuteNonQuery()
                    conn.Close()

                    //'Need to delete from 
                    gvMyTableMaintenance.DeleteRow(gvMyTableMaintenance.FocusedRowHandle)
                Catch ex As Exception
                    //'Handle Exception
                End Try
            End Using
        End Using
    End If
    e.Handled = True
End Sub

2 个答案:

答案 0 :(得分:2)

从我的角度来看,您选择了正确的方法来实现此功能。

答案 1 :(得分:0)

以下是我最终代码的基本版本:

Private Sub gcMyTableMaintenance_EmbeddedNavigator_ButtonClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraEditors.NavigatorButtonClickEventArgs) Handles gcMyTableMaintenance.EmbeddedNavigator.ButtonClick
    If e.Button.ButtonType = DevExpress.XtraEditors.NavigatorButtonType.Remove Then
        Dim iMyTableId As Int32 = 0

        iMyTableId = gvMyTableMaintenance.GetFocusedRowCellValue("id")

        Using conn As New SqlConnection(MyConnectionString)
            Using lCmd As New SqlCommand()
                Try
                    lCmd.Connection = conn
                    lCmd.CommandType = CommandType.StoredProcedure
                    lCmd.CommandText = "ExpireFromMyTable"
                    lCmd.Parameters.AddWithValue("@myTableId", iMyTableId)
                    conn.Open()
                    lCmd.ExecuteNonQuery()
                    conn.Close()

                    //'Need to delete from 
                    gvMyTableMaintenance.DeleteRow(gvMyTableMaintenance.FocusedRowHandle)
                Catch ex As Exception
                    //'Handle Exception
                End Try
            End Using
        End Using
    End If
    e.Handled = True
End Sub