Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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
as
 
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.
 

 
Thanks,
Tirupati Jogu
Posted 16-Nov-11 7:38am
Edited 16-Nov-11 7:52am
DaveAuld172.7K
v2

1 solution

Rate this: bad
good
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";
dataGridView1.Columns.Add(textColumn0);
textColumn0.SortMode = DataGridViewColumnSortMode.Programmatic;
textColumn0.HeaderCell.SortGlyphDirection = SortOrder.None;
 
DataGridViewTextBoxColumn textColumn1 = new DataGridViewTextBoxColumn();
textColumn1.DataPropertyName = "Column2";
dataGridView1.Columns.Add(textColumn1);
textColumn1.SortMode = DataGridViewColumnSortMode.Programmatic;
textColumn1.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
 
DataGridViewTextBoxColumn textColumn2 = new DataGridViewTextBoxColumn();
textColumn2.DataPropertyName = "Column3";
dataGridView1.Columns.Add(textColumn2);
textColumn2.SortMode = DataGridViewColumnSortMode.Programmatic;
textColumn2.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
 
dataGridView1.DataSource = BS;
 

 

It's Help for u...
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 16 Nov 2011
Copyright © CodeProject, 1999-2014
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