2010-01-07 144 views
3

是否有一種簡單的方法來重新排序在運行時綁定到DataSet的DataGridView的列?.Net重新排序DataGridView運行時列

我可以在設計時通過DataGridView「編輯列」對話框來完成它,但必須記住每次對錶單進行更改(將DataGridView刪除或添加到表單中)時都要這樣做。

感謝

回答

5

使用DisplayIndex

' Swap the last column with the first.' 
Private Sub Button10_Click(ByVal sender As Object, _ 
    ByVal args As EventArgs) Handles Button10.Click 

    Dim columnCollection As DataGridViewColumnCollection = _ 
     dataGridView.Columns 

    Dim firstVisibleColumn As DataGridViewColumn = _ 
     columnCollection.GetFirstColumn(DataGridViewElementStates.Visible) 
    Dim lastVisibleColumn As DataGridViewColumn = _ 
     columnCollection.GetLastColumn(DataGridViewElementStates.Visible, _ 
     Nothing) 

    Dim firstColumn_sIndex As Integer = firstVisibleColumn.DisplayIndex 
    firstVisibleColumn.DisplayIndex = _ 
     lastVisibleColumn.DisplayIndex 
    lastVisibleColumn.DisplayIndex = firstColumn_sIndex 
End Sub 
+0

感謝,這正是我要尋找 – Belliez 2010-01-11 09:51:10