Sorting in dataview not happening properly(C#3.0)
I have the following
DataTable dt = new DataTable();
dt.Columns.Add("col开发者_如何学编程1", typeof(string));
dt.Columns.Add("col2", typeof(string));
dt.Rows.Add("1", "r1");
dt.Rows.Add("1", "r2");
dt.Rows.Add("1", "r3");
dt.Rows.Add("1", "r4");
dt.Rows.Add("2", "r1");
dt.Rows.Add("2", "r2");
dt.Rows.Add("2", "r3");
dt.Rows.Add("2", "r4");
dt.Rows.Add("3", "r1");
dt.Rows.Add("3", "r2");
dt.Rows.Add("3", "r3");
dt.Rows.Add("3", "r4");
dt.Rows.Add("1", "r1");
dt.Rows.Add("1", "r2");
DataView dv = new DataView(dt);
dv.Sort = "col1";
DataTable dResult = dv.Table;
I am trying to sort the datatable with the help of dataview so that the result will be
1 r1
1 r2
...
2 r1
2 r2
...
3 r1
3 r2
......
Means all 1's first followed by 2's and 3r's
Even I tried with dt.DefaultView.Sort = "col1";
but no luck.
But it is not happening. Only the result i am able to view in dv.Sort
and not the datatable (dResult)
I am using C#3.0.
Please help
Thanks
Change the type of the 'col1' column to int instead of string.
Then enter those values as integers instead.
-Edit
Yea, that doesn't work.
dv.Table returns the Source table, not the sorted data.
Use this to get the sorted data.
DataTable dResult = dv.ToTable();
精彩评论