Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi fellow programmers,

I have a dataset having table names like
aa002500T0,hb004400T0,ba000300T0,ba000100T0,...
is it possible to change there order in ascending like below in front end
aa002500T0,ba000100T0,ba000300T0,hb004400T0,...

thanks & regards
george
Posted
Comments
dan!sh 13-Jan-15 0:54am    
Why would you need to do that?
george4986 13-Jan-15 1:14am    
i am using a 3rd party reporting tool,by default it loads dataset tables without order
dan!sh 13-Jan-15 1:16am    
And why does the order matter? You know the names of all the tables so you can access data whenever needed.
george4986 13-Jan-15 1:17am    
yes but my client needs the ordering
dan!sh 13-Jan-15 1:18am    
Why? Sorry, but this just does not make sense to me. Why does it matter how tables are stored in dataset?

Based on your responses to the other comments and solutions I think this might do what you need ...

My "sample data":
C#
DataSet ds = new DataSet();
ds.Tables.Add("aTable");
ds.Tables.Add("cTable");
ds.Tables.Add("bTable");

I use CopyTo to get the tables within the dataset into an array - on which I can then use Linq
C#
DataTable[] dta = new DataTable[ds.Tables.Count];
ds.Tables.CopyTo(dta, 0);
Here's the actual sort
C#
foreach (DataTable t in dta.OrderBy(c => c.TableName))
      Console.WriteLine(t.TableName);
which gives me the output
aTable
bTable
cTable
as expected

Edit - Forgot to mention to ensure you have included
using System.Linq;
 
Share this answer
 
v2
Comments
george4986 16-Jan-15 0:32am    
Awesome it works , thanks CHill ;-)
my code is
DataSet ds = objCon.DataSet;
DataTable[] dta = new DataTable[ds.Tables.Count];
ds.Tables.CopyTo(dta, 0);
ds.Tables.Clear();
foreach (DataTable t in dta.OrderBy(c => c.TableName))
{
ds.Tables.Add(t);
}
Linq will simplify the stuff, using linq you can get many functionalities similar to sql
hope it helps you.

You can use orderby in linq

Find the sample below

http://msdn.microsoft.com/en-us/library/vstudio/bb534966%28v=vs.100%29.aspx[^]

Regards,
Santosh
 
Share this answer
 
v2
Comments
george4986 15-Jan-15 5:00am    
I will check the link.thanks for ur effort.use improve solution option & plz dont post multiple answer threads.
santosh.yadav198613 15-Jan-15 6:01am    
oops sorry did it by mistake.
CHill60 15-Jan-15 5:01am    
Dataset doesn't implement IEnumerable - I don't think Linq will work in this instance
santosh.yadav198613 15-Jan-15 5:58am    
system.data.datasetextensions use this dll into your dataaccesslayer to support IEnumerable.
CHill60 15-Jan-15 6:39am    
I can only see how that would work on the tables themselves not the dataset. Could you provide an example of how you would get it to work?
you can use below query:

select * from sys.tables

hope it will help.

Regards,
Santosh
 
Share this answer
 
Comments
george4986 15-Jan-15 4:37am    
is it possible in front end? i know its possible in backend.
thanks for ur reply.
 
Share this answer
 
Comments
george4986 15-Jan-15 4:39am    
i have checked ur links , i know its possible to filter/sort contents of tables in a dataset . but my requirement is ordering tablenames in dataset.hope its clear to u.thanks for the effort.

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