Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am having a list of DB objects in a datatable.
C#
ID ObjectType, ObjectName
1 StoredProcedure, usp_getList
2 Table, tbl_Users
3 Function, udf_getNames
4 View, v_Changes 
5 Table, tbl_Admin
6 Function, udf_SplitString


I need to group them by each object type and then sort it according to the object name.
C#
ID ObjectType, ObjectName
1 Function, udf_getNames
2 Function, udf_SplitString
3 StoredProcedure, usp_getList
4 Table, tbl_Admin
5 Table, tbl_Users
6 View, v_Changes 


I tried the following code, but it seems not working

C#
DataView dv = new DataView(tbl);
           dv.Sort = "ObjectType ASC";

           var groupbyfilter = from d in tbl.AsEnumerable() group d by d["ObjectType "];
           foreach (var x in groupbyfilter)
               x.CopyToDataTable(tbl, LoadOption.OverwriteChanges);


Please help resolve this. Thanks in advance.
Posted
Updated 6-Aug-13 23:58pm
v3

1 solution

Try this:
C#
DataTable dt = new DataTable();
DataColumn ID = new DataColumn();
ID.ColumnName = "ID";
ID.DataType = System.Type.GetType("System.Int32");
ID.AutoIncrement = true;
ID.AutoIncrementSeed = 1;
ID.AutoIncrementStep = 1;

dt.Columns.Add(ID);
dt.Columns.Add("ObjectType", typeof(string));
dt.Columns.Add("ObjectName", typeof(string));

DataTable copy = tbl.Copy();
copy.Columns.Remove("ID");
copy.DefaultView.Sort = "ObjectType" + " " + "ASC";
copy = copy.DefaultView.ToTable();

foreach(DataRow drow in copy.AsEnumerable())
{
    dt.ImportRow(drow);
}

Now you can use dt as source for your dataview.
 
Share this answer
 
Comments
ridoy 7-Aug-13 6:00am    
good one,+5
Kuthuparakkal 7-Aug-13 6:01am    
Thanks Ridoy!!!
ridoy 7-Aug-13 6:03am    
wc.

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