how can I sort DataGrid programatically ado.net

databases Poland
  • 12 years ago
    Hello,

    I am writing a program in vb.net using ado.net. I have such a problem:
    how can I sort DataGrid programatically???

    The problem is that I merge few datasets and get one so I can't sort it using 'order by' in sql.

    Please help.

    Thanks in advance for your help.
  • 12 years ago

    Try this code and see if it helps, obviously you will have to change according to your circumstances:


    Code:

               Me.DataGrid1.DataSource = ds.Tables("PRICES") ' sort applied in SQL when filling dataset
               VarCurrencyManager = CType(BindingContext(DataGrid1.DataSource), CurrencyManager)
               DataView1 = CType(VarCurrencyManager.List, DataView)


               ' displaying the results in datagrid1 * not allowing edit or addnew rows *
               DataView1.AllowNew = False
               DataView1.AllowEdit = False
               DataView1.AllowDelete = False


               Dim tableStyle As New DataGridTableStyle()
               tableStyle.MappingName = ds.Tables(0).TableName.ToString


               Dim column As New DataGridTextBoxColumn()
               column.MappingName = "COMPANYPRICINGITEM_ID" 'Table Column Name
               column.HeaderText = "PricingID" 'Datagrid colum name
               column.Alignment = HorizontalAlignment.Center
               column.Width = 75
               tableStyle.GridColumnStyles.Add(column)


               column = New DataGridTextBoxColumn()
               column.MappingName = "COMPANYSERVICEID"
               column.HeaderText = "SvcID"
               column.Alignment = HorizontalAlignment.Center
               column.Width = 75
               tableStyle.GridColumnStyles.Add(column)


               column = New DataGridTextBoxColumn()
               column.MappingName = "COMPANY_ID"
               column.HeaderText = "CoID"
               column.Alignment = HorizontalAlignment.Center
               column.Width = 75
               tableStyle.GridColumnStyles.Add(column)


               column = New DataGridTextBoxColumn()
               column.MappingName = "COMPANY_NAME"
               column.HeaderText = "Haulier"
               column.Alignment = HorizontalAlignment.Left
               column.Width = 200
               tableStyle.GridColumnStyles.Add(column)


               column = New DataGridTextBoxColumn()
               column.MappingName = "COMPANYSERVICEITEM"
               column.HeaderText = "Rate Type"
               column.Width = 100
               tableStyle.GridColumnStyles.Add(column)


               column = New DataGridTextBoxColumn()
               column.MappingName = "COMPANY_DESCRIPTION"
               column.HeaderText = "Svc"
               column.Width = 150
               tableStyle.GridColumnStyles.Add(column)


               column = New DataGridTextBoxColumn()
               column.MappingName = "COST"
               column.HeaderText = "Unit Rate"
               column.Width = 75
               tableStyle.GridColumnStyles.Add(column)



               Me.DataGrid1.TableStyles.Clear()
               Me.DataGrid1.TableStyles.Add(tableStyle)


Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC. As potential programmers, they are mentally mutilated beyond hope of regeneration.” - E. W. Dijkstra