简体   繁体   English

有没有办法在使用JOIN子句时停止列的自动排序

[英]Is there a way to stop the auto sorting of the columns when using JOIN clause

I'm setting up this datagridview with lots of data from different data tables so I used JOIN. 我正在使用来自不同数据表的大量数据设置此datagridview,因此我使用了JOIN。 However, the datagridview seems to display the columns in order of the table. 但是,datagridview似乎按表的顺序显示列。

This is my code: 这是我的代码:

 Using conn As New MySqlConnection(connString)
            conn.Open()
            Using cmd As New MySqlCommand("SELECT * FROM tblclientprofile LEFT JOIN tblbusinessinfo ON tblbusinessinfo.B_IdNo = tblclientprofile.IdNo 
                                            LEFT JOIN tblownerprofile ON tblownerprofile.O_IdNo = tblbusinessinfo.B_IdNo
                                            LEFT JOIN tblbusinessprofile ON tblbusinessprofile.BP_IdNo = tblownerprofile.O_IdNo
                                            LEFT JOIN tblbusinessoperations ON tblbusinessoperations.BO_IdNo = tblbusinessprofile.BP_IdNo
                                            LEFT JOIN tblbfs ON tblbfs.BF_IdNo = tblbusinessoperations.BO_IdNo
                                            LEFT JOIN tblcapitalstructure ON tblcapitalstructure.IdNo = tblbfs.BF_IdNo
                                            LEFT JOIN tblassetsize ON tblassetsize.IdNo = tblcapitalstructure.IdNo
                                            LEFT JOIN tblsaleshistory ON tblsaleshistory.IdNo = tblassetsize.IdNo
                                            LEFT JOIN tbldomesticmarket ON tbldomesticmarket.IdNo = tblsaleshistory.IdNo
                                            LEFT JOIN tblexportmarket ON tblexportmarket.IdNo = tbldomesticmarket.IdNo
                                            LEFT JOIN tblprodservline ON tblprodservline.IdNo = tblexportmarket.IdNo
                                            ", conn)
                With cmd
                    .CommandType = CommandType.Text
                End With
                Using da As New MySqlDataAdapter
                    da.SelectCommand = cmd
                    Using dt As New DataTable
                        da.Fill(dt)
                        dgv2.DataSource = dt
                    End Using
                End Using
            End Using
        End Using

tblclientprofile has fields such as: IdNo, Status, Name, Position, etc tblbusinessinfo has fields such as: BusinessRegNo, BusinessName, BusinessType etc. tblclientprofile具有以下字段:IdNo,Status,Name,Position等.tblbusinessinfo具有以下字段:BusinessRegNo,BusinessName,BusinessType等。

datagridview's results is IdNo, Status, Name, Position, BusinessRegNo, BusinessName, BusinessType in order. datagridview的结果依次为IdNo,Status,Name,Position,BusinessRegNo,BusinessName,BusinessType。

What i want is to display this in order of: IdNo, Name, Position, BusinessRegNo, BusinessName, BusinessType, Status 我想要的是按以下顺序显示:IdNo,Name,Position,BusinessRegNo,BusinessName,BusinessType,Status

My simple solution to the columns being out of order is to add this loop that sets the DisplayIndex to the Index . 我对列乱序的简单解决方案是添加将DisplayIndex设置为Index循环。 Put this loop right after your call dgv2.DataSource = dt 在调用dgv2.DataSource = dt之后立即放置此循环

For Each col As DataGridViewColumn In dgv2.Columns 
    col.DisplayIndex = col.Index
Next

The Index is assigned to each column as they are added. Index将在添加时分配给每个列。 I'm not sure why the DisplayIndex becomes out of order, but the above script will fix it. 我不确定为什么DisplayIndex会出现乱序,但上面的脚本会修复它。

Also, I'd like to point out that this has nothing to do with the SQL. 另外,我想指出这与SQL无关。 This is the result of something going on in the DataGridView. 这是DataGridView中发生的事情的结果。 With that said, you should still select your columns instead of using *. 话虽如此,您仍然应该选择列而不是使用*。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM