Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort a datagrid column 1

Status
Not open for further replies.

696796

Programmer
Aug 3, 2004
218
GB
hi i have a datagrid all set up, i just need to know how to sort on one of the columns, for example supplierName...

I've put my code below to show you how i have it set up.

Cheers, al

Code:
        dtAudits = ds.Tables("audits")



        ' present data
        With dgAudits
            .DataSource = dtAudits
            .ReadOnly = True
            ' Set DataGrid Data Binding to Machines table
            .SetDataBinding(ds, "audits")
            ' Set DataGrid Background Color
            .BackgroundColor = System.Drawing.Color.Gray
            ' Set DataGrid Caption Background Color
            .CaptionBackColor = System.Drawing.Color.DarkBlue
            ' Set DataGrid Caption Foreground Color
            .CaptionForeColor = System.Drawing.Color.White
            ' Set DataGrid Parent Rows Background Color
            .ParentRowsBackColor = System.Drawing.Color.DarkBlue
            ' Set DataGrid Parent Rows Foreground Color
            .ParentRowsForeColor = System.Drawing.Color.SlateBlue
            ' Set DataGrid Caption Text
            .CaptionText = "Audits Results"
            ' Clear DataGrid Table Styles
            .TableStyles.Clear()
        End With

        ' Set up data grid Table Style
        Dim tableStyle As New DataGridTableStyle
        tableStyle.MappingName = "audits"



        ' Set data grid Table Style
        Dim TblStyle As New DataGridTableStyle

        With TblStyle

            .MappingName = "audits"
            .BackColor = System.Drawing.Color.White
            .AlternatingBackColor = Color.LightBlue
            .ForeColor = System.Drawing.Color.DarkSlateBlue
            .GridLineColor = System.Drawing.Color.MediumSlateBlue
            .HeaderBackColor = System.Drawing.Color.Lavender
            .HeaderForeColor = System.Drawing.Color.Black
            .RowHeaderWidth = 10


            ' Set column styles
            With .GridColumnStyles

                .Add(New DataGridTextBoxColumn)
                With .Item(0)
                    .MappingName = "auditId"
                    .HeaderText = "Audit ID"
                    .Width = 0
                    .NullText = String.Empty
                End With

                .Add(New DataGridTextBoxColumn)
                With .Item(1)
                    .MappingName = "supplierName"
                    .HeaderText = "Supplier"
                    .Width = 190
                    .NullText = String.Empty
                End With

 
You can sort a datagrid in a couple of ways. The first would be through your SQL Select statemnt (if you are in fact using SQL to load the grid.) You would use Order By in the Select statement.

The second way is to set the allow sort property of the grid to True. This will allow you to sort by any column in the grid.
 
cool, can i get the sort property to sort a column when i load the datagrid?
 
its cool, just orddered the sql like u said
 
Or you can assign it to a dataview and do a

Code:
dv.sort = "Colname"

and bind to the dataview. This way is very easy to manipulate.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top