none
Apply DGV Columns position change ? RRS feed

  • Question

  • Greetings ,

    I change position of my dgv's columns via code below :

    With DGV1

    .Columns(1).DisplayIndex = 1

    .Columns(2).DisplayIndex = 2

    ...

    End With

    Then I  populate a datatable with dgv datasource. But it doesn't apply the changes to columns' position. It uses the original DGV  .

    I would be thankful if you help me.

    Sunday, September 30, 2018 9:54 PM

All replies

  • Hello,

    Try setting the ordinal position of the DataColumn rather than the DataGridViewColumn if you have not created DataGridViewColumns in the IDE.

    So if I have the following DataTable where Id is position 0.

    Public Function GetAll() As DataTable
        Dim dt As New DataTable
        Using cn As New OleDbConnection(Builder.ConnectionString)
            Using cmd As New OleDbCommand("", cn)
                cmd.CommandText = "SELECT id,UserName,UserPassword FROM Users"
                cn.Open()
                dt.Load(cmd.ExecuteReader)
            End Using
        End Using
        Return dt
    End Function

    Then set it's ordinal position as follows (note the BindingSource is optional but has many features)

    Private bs As New BindingSource
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim ops As New Operations
        Dim dtUsers = ops.GetAll
    
        dtUsers.Columns("id").SetOrdinal(1)
        bs.DataSource = dtUsers
        DataGridView1.DataSource = bs
        DataGridView1.Columns("id").ReadOnly = True
    End Sub

    Back to your situation, I can reproduce it. Given

    With this code

    Private bs As New BindingSource
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim ops As New Operations
        Dim dtUsers = ops.GetAll
    
        DataGridView1.AutoGenerateColumns = False
        bs.DataSource = dtUsers
        DataGridView1.DataSource = bs
        DataGridView1.Columns("IdColumn").DisplayIndex = 1
        DataGridView1.Columns("id").ReadOnly = True
    End Sub
    

    I get what I asked for


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, September 30, 2018 10:37 PM
    Moderator
  • Greetings ,

    I change position of my dgv's columns via code below :

    With DGV1

    .Columns(1).DisplayIndex = 1

    .Columns(2).DisplayIndex = 2

    ...

    End With

    Then I  populate a datatable with dgv datasource. But it doesn't apply the changes to columns' position. It uses the original DGV  .

    I would be thankful if you help me.

    You have to turn off AutoGenerateColumns after you set the datasource, then your code will work as is

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Monday, October 1, 2018 2:26 PM