sort column in datagrid asc...

banks

Well-known member
Joined
Sep 7, 2005
Messages
50
Programming Experience
Beginner
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

VB.NET:
        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
 
the best way to sort is to have the SQL statement pull the records sorted by the column you want (it can be sorted Asc and Dsc) in the DataAdapter
 
is that Asp.net or vb.net. If you r using asp.net then it is very simple right click the grid select property builder and see the general options and tick to the allow sorting option. or dgAudits.allowsorting = true then write the required code in the dgaudits_SortCommand() event.
thx
 
Back
Top