Click here to Skip to main content
13,051,404 members (45,751 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi ,

I want to sort/ order by datatable's multiple fields at a time.
LIke we do in Sql Server "Select EmpCode,EmpName,Date from Employee order Empcode,Date"

Same way i want to sort two or three columns of datatable.

I tried Datatable's dataview

Dim dv as New Dataview(datatbale)
dv.sort= "EmpCode,Date ASC"  -- this sorts only first column
and also
dv.sort = "EmpCode ASC,Date ASC" -- this returns an error

Please reply for the above query.

Tirupati Jogu
Posted 16-Nov-11 7:38am
Updated 16-Nov-11 7:52am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

BindingSource BS = new BindingSource();
DataTable testTable = new DataTable();
testTable.Columns.Add("Column1", typeof(int));
testTable.Columns.Add("Column2", typeof(string));
testTable.Columns.Add("Column3", typeof(string));

testTable.Rows.Add(1, "Value1", "Test1");
testTable.Rows.Add(2, "Value2", "Test2");
testTable.Rows.Add(2, "Value2", "Test1");
testTable.Rows.Add(3, "Value3", "Test3");
testTable.Rows.Add(4, "Value4", "Test4");
testTable.Rows.Add(4, "Value4", "Test3");

DataView view = testTable.DefaultView;
view.Sort = "Column2 ASC, Column3 ASC";// Sorting Column 2 and column 3
BS.DataSource = view;

DataGridViewTextBoxColumn textColumn0 = new DataGridViewTextBoxColumn();
textColumn0.DataPropertyName = "Column1";
textColumn0.SortMode = DataGridViewColumnSortMode.Programmatic;
textColumn0.HeaderCell.SortGlyphDirection = SortOrder.None;

DataGridViewTextBoxColumn textColumn1 = new DataGridViewTextBoxColumn();
textColumn1.DataPropertyName = "Column2";
textColumn1.SortMode = DataGridViewColumnSortMode.Programmatic;
textColumn1.HeaderCell.SortGlyphDirection = SortOrder.Ascending;

DataGridViewTextBoxColumn textColumn2 = new DataGridViewTextBoxColumn();
textColumn2.DataPropertyName = "Column3";
textColumn2.SortMode = DataGridViewColumnSortMode.Programmatic;
textColumn2.HeaderCell.SortGlyphDirection = SortOrder.Ascending;

dataGridView1.DataSource = BS;

It's Help for u...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 16 Nov 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100