Column order in Datagridview with LinkToSQL

Bob Langlade

Well-known member
Joined
Sep 20, 2005
Messages
115
Location
Saint-Pierre & Miquelon Islands
Programming Experience
3-5
Hi,

I am trying Link to SQL in VB 2008 as it seems a promising feature.

I have added a LinkToSql class (name Sage.dbml) to my project with all the databases in needed from my SQL Server.

I then perform this simple code to populate my datagridview :

VB.NET:
 Dim Db As New SageDataContext
        Db.DeferredLoadingEnabled = False
        Dim Elements = From Customers In Db.F_COMPTET _
                      Select Number = Customers.CT_Num, Name = Customers.CT_Intitule, MaxCredit = Customers.CT_Encours _
                      Where (Name.Contains(TextBox1.Text))
        DataGridView1.DataSource = Elements

But instead of having columns created in the order "Number, Name, MaxCredit", I get "MaxCredit, Name, Number", which looks like by selecting a LinkToSql object as datasource you automatically sort alphabetically your column header.

Any idea on how to fix that?
 

Attachments

  • ScreenPrint.gif
    ScreenPrint.gif
    27.6 KB · Views: 40
Same problem here :(

As I show the LINQ object return the columns ordered as I typed...

when 'give' the object to datagridview, mess it up... :(((((((
 
I eventually got the answer on a French Forum, you can modify the position of the columns by using the DisplayIndex property::)

VB.NET:
        Dim Db As New SageDataContext
        Db.DeferredLoadingEnabled = False
        Dim Elements = From Customers In Db.F_COMPTET _
                      Select Number = Customers.CT_Num, Name = Customers.CT_Intitule, MaxCredit = Customers.CT_Encours _
                      Where (Name.Contains(TextBox1.Text))
        DataGridView1.DataSource = Elements
        With DataGridView1.Columns
            .Item("Number").DisplayIndex = 0
            .Item("Name").DisplayIndex = 1
            .Item("MaxCredit").DisplayIndex = 2
        End With
 
Back
Top