1
votes

In some cases (I believe when inner join is used) datagridview does not represent correctly the column order populated from SQL query.

For example, I query the table columns in this order:

Select Booking_ID, Client_Name, Room_ID 

although SQL return columns in correct order, however datagridview displays columns automatically in different order:

Room_ID || Booking_ID || Client_Name

The question is, how to disable this automatic column horizontal ordering, so datagridview displays the table exactly how SQL does?

I am using datasource method to fill the datagrid.

SQLDA = New SqlDataAdapter(SqlCmd)
SQLDataset = New DataSet
SQLDA.Fill(SQLDataset)
Form2.DGVData.DataSource = SQLDataset.Tables(0)

In most cases this problem is not present, and datagridview shows the table exactly as SQL does.

Thank you in advance.

PS: I would post the images but I'm not allowed since 10 reputation is required.

1
As Question is not clear....How you filling datagridview ? show the code also .. if your using datasource method to fill then try to use order by in query,i.e Select Booking_ID, Client_Name, Room_ID from yourTable order by Client_Name - Vivek S.
Yes I am using datasource method. SQLDA = New SqlDataAdapter(SqlCmd) SQLDataset = New DataSet SQLDA.Fill(SQLDataset) Form2.DGVData.DataSource = SQLDataset.Tables(0) - user3707554
show your code to fill datagridview edit your question - Vivek S.
'order by' command order the rows, in my case row order is not the problem, instead column order is incorrect as I explained above. - user3707554
To answer your question, check here.. To setthe Display Index, see here - Mark C.

1 Answers

1
votes

Simple, but should work:

dgvDataGridView.Columns("Booking_ID").DisplayIndex = 0
dgvDataGridView.Columns("Client_Name").DisplayIndex = 1
dgvDataGridView.Columns("Room_ID").DisplayIndex = 2
'...