2015-08-27 31 views
0

如此DevExpress thread中所述,我可以上下移動網格行。但只要我從數據庫中添加表主鍵約束,它停止工作。投擲異常:XtraGrid上下移動網格行

列'Id'被限制爲唯一。值「101」已經存在

這是我下移代碼:

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 

至於這個thread,我還沒有試過,但當然,如果列數更高,我有遍歷所有專欄。即循環。

感謝

編輯1請,不拖/放碼。我正在按鈕點擊。


EDIT 2 有一個問題!我有暗淡可見的行展開按鈕。就像,如果存在子行,那麼展開按鈕將是正確可見的,但是如果不正常的網格行爲則會變暗。但是當交換行時,展開按鈕不會改變它們的狀態,這是錯誤的並且是不希望的。你能解決這個問題嗎?

回答

0

您需要使用DataTable.BeginLoadData方法和DataTable.EndLoadData方法。 DataTable.BeginLoadData方法在加載數據時關閉通知,索引維護和約束,並且DataTable.EndLoadData將其打開。
這裏是例子:

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 你需要更新您的使用GrdCntrlMain.RefreshDataSource,所以在你的基礎數據源的變化將被正確地反映在您的DataControlGridControl

+0

謝謝,它正在工作!據此https://msdn.microsoft.com/en-us/library/s3bxwk8b.aspx你也可以使用'dataSet1.EnforceConstraints = False',之後,'dataSet1.EnforceConstraints = True' –

+0

@FaizanMubasher是的,但在你的問題沒有關於DataSet的信息。 – nempoBu4

+0

我知道!我剛剛在你的回答之前找到了它:-)。沒關係 –