Search code examples
.netvb.netdevexpressxtragrid

XtraGrid Move Grid Row Up and Down


As described in this DevExpress thread, I was able to move grid rows up and down. But as soon as I added table from database with Primary Key constraint, it stopped working. Throwing exception:

Column 'Id' is constrained to be unique. Value '101' is already present

This is my MoveDown code:

Private Sub BtnMveDwn_Click(sender As Object, e As EventArgs) Handles BtnMveDwn.Click

    Dim lObjGrdVew As GridView
    Dim lObjDtaTbl As DataTable
    Dim lIntIndex As Integer
    Dim lObjTmpRow As Object
    Try
        lObjGrdVew = CType(GrdCntrlMain.FocusedView, GridView)
        lObjDtaTbl = TryCast(GrdCntrlMain.DataSource, DataTable)
        lIntIndex = lObjGrdVew.FocusedRowHandle

        lObjGrdVew.GridControl.Focus()

        If lIntIndex >= lObjGrdVew.DataRowCount - 1 Then
            Return
        End If

        lObjTmpRow = lObjDtaTbl(lObjGrdVew.ViewRowHandleToDataSourceIndex(lIntIndex + 1)).ItemArray

        lObjDtaTbl(lObjGrdVew.ViewRowHandleToDataSourceIndex(lIntIndex + 1)).ItemArray = lObjDtaTbl(lObjGrdVew.ViewRowHandleToDataSourceIndex(lIntIndex)).ItemArray

        lObjDtaTbl(lObjGrdVew.ViewRowHandleToDataSourceIndex(lIntIndex)).ItemArray = lObjTmpRow

        lObjGrdVew.FocusedRowHandle += 1
        lObjGrdVew.UnselectRow(lObjGrdVew.FocusedRowHandle - 1)
        lObjGrdVew.SelectRow(lObjGrdVew.FocusedRowHandle)

    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

End Sub

As for this thread, I haven't tried but of course if number of columns are higher, I have to go through all columns. i.e, Looping.

Thanks

EDIT 1 Please, no drag/drop code. I am doing it on Button click.


EDIT 2 There is a problem! I have rows with dim and visible Expand buttons. Like, if there is exist child row then expand button will be proper visible but dim otherwise just like normal grid behavior. But when rows are swapped, expand buttons doesn't change their state which is wrong and undesirable. Can you sort out the issue?


Solution

  • You need to use DataTable.BeginLoadData method and DataTable.EndLoadData method. The DataTable.BeginLoadData method turns off notifications, index maintenance, and constraints while loading data, and DataTable.EndLoadData turns its on.
    Here is example:

    Private Sub BtnMveDwn_Click(sender As Object, e As EventArgs) Handles BtnMveDwn.Click
    
        Dim lObjGrdVew As GridView
        Dim lObjDtaTbl As DataTable
        Dim lIntIndex As Integer
        Dim lObjTmpRow As Object
        Try
            lObjGrdVew = CType(GrdCntrlMain.FocusedView, GridView)
            lObjDtaTbl = TryCast(GrdCntrlMain.DataSource, DataTable)
            lIntIndex = lObjGrdVew.FocusedRowHandle
    
            lObjGrdVew.GridControl.Focus()
    
            If lIntIndex >= lObjGrdVew.DataRowCount - 1 Then
                Return
            End If
    
            lObjTmpRow = lObjDtaTbl(lObjGrdVew.ViewRowHandleToDataSourceIndex(lIntIndex + 1)).ItemArray
    
            lObjDtaTbl.BeginLoadData '<= turn off constraints
    
            lObjDtaTbl(lObjGrdVew.ViewRowHandleToDataSourceIndex(lIntIndex + 1)).ItemArray = lObjDtaTbl(lObjGrdVew.ViewRowHandleToDataSourceIndex(lIntIndex)).ItemArray
    
            lObjDtaTbl(lObjGrdVew.ViewRowHandleToDataSourceIndex(lIntIndex)).ItemArray = lObjTmpRow
    
            lObjDtaTbl.EndLoadData '<= turn on constraints
    
            GrdCntrlMain.RefreshDataSource() '<= update GridControl to reflect the changes
    
            lObjGrdVew.FocusedRowHandle += 1
            lObjGrdVew.UnselectRow(lObjGrdVew.FocusedRowHandle - 1)
            lObjGrdVew.SelectRow(lObjGrdVew.FocusedRowHandle)
    
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    
    End Sub
    

    EDIT 2 You need to update your GridControl by using GrdCntrlMain.RefreshDataSource, so the changes in your underlying data source will be correctly reflected in your DataControl.