Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
as

VB
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
Updated 16-Nov-11 7:52am
v2

1 solution

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...
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900