Reorder columns of unbound datagridview

Juan Velasquez
Juan Velasquez used Ask the Experts™
on
Hello,
I am using the attached code to set the datasource of an unbound datagrid view at runtime.  How can I change the column order.


Try
            dgvAERGrid.DataSource = AERDB.GetAERList
        Catch ex As SqlException
            MessageBox.Show("Database error retrieving data from database.", "Database Error")
        Catch ex As Exception
            MessageBox.Show(ex.Message, ex.GetType.ToString)
        End Try

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I figured out the solution.  I inserted the following code
dgvLtblAER.Columns(1).DisplayIndex = 2

  Private Sub PopulateGridView()
        Try
            Dim al As New SortableBindingList(Of AERLookup)(AERDB.GetAERList)
            dgvLtblAER.DataSource = al
            dgvLtblAER.Columns(0).Visible = False
            dgvLtblAER.Columns(1).DisplayIndex = 2
        Catch ex As Exception
            MessageBox.Show(ex.Message, ex.GetType.ToString)
        End Try
    End Sub

Open in new window

Senior Software Engineer
Commented:
you can do this as well:

remove your line 5

Private Sub PopulateGridView()
        Try
            Dim al As New SortableBindingList(Of AERLookup)(AERDB.GetAERList)
            dgvLtblAER.DataSource = al
            dgvLtblAER.Columns(1).DisplayIndex = 2
        Catch ex As Exception
            MessageBox.Show(ex.Message, ex.GetType.ToString)
        End Try
    End Sub

Open in new window

Author

Commented:
Although I had come up with the solution, I'm awarding the point to you because of your assist

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial