473,803 Members | 3,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sort a datatable and create a new datatable

All,

I've been looking around for a solution to my problem for a couple of days
now.

In short, here's my situation:

1.
I'm getting a result from a component back as a datatable and I have no
option to sort the different columns during the call to the component.

Code-snippet:
Dim dTable As DataTable

Dim x As New MyComponent

'---between these two lines I add some parameters and properties
....x.blabla = ""
'---

2.
I then execute the component:

dTable = x.Execute()

So faar so good. No SQL invloved (only within the component).

Now comes the tricky part. I need to sort the result and display it.
However, I can't just use the .Sort in DataRow and get a 1-D array back. I
have properties in the component that I need to get back via an index. I've
already tried to re-build it using the array but it's simply not working.

So I thought I could create a 1-D array and then copy it to a second
DataTable. Something like this:

dTable.Execute( )
Dim dRow As DataRow
dRow = dTable.Select(N OTHING, "COLUMN_NAM E ASC, COLUMN_NAME2 DESC")

I now have a 1D array that contains my sorted data from the DataTAble
Now I would like to create a new DataTable from the 1D array.

Does anybody have any idea how to solve it?

Thanks in advance,
Fredrik
Nov 18 '05 #1
2 3180
"Fredrik Rodin" <ro***@dacsa.ne t> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
<snip>
Now comes the tricky part. I need to sort the result and display it.
However, I can't just use the .Sort in DataRow and get a 1-D array back. I
have properties in the component that I need to get back via an index. I've already tried to re-build it using the array but it's simply not working.


I'm afraid I don't know what you mean in the above paragraph.

Without understading that, I'll still venture to guess that you should use a
DataView to see a sorted view of your DataTable. That is:

DataView dv = new DataView(table)
dv.Sort = (sort expression)

If you have rows that you need to get to via an index (like you want the
53rd row of the sorted dataview) then things get complicated. The main way
to access the rows is through the GetEnumerator() method. You'd then have to
call .MoveNext() 53 times before using Current to get the row. You could
build a simple method to do that, and I doubt you'll notice any performance
problems unless you're calling it A LOT.

If you are calling that function a lot to index by rows (and you aren't
sorting very often), then it might make sense to copy all the data to a new
table (but I don't think that makes sense if you are sorting a lot). If you
decide a second table is worth it, then you'll probably have to write some
code like the following to copy a sorted dataview to a new table.

DataTable sortedTable = new DataTable();
foreach (DataColumn col in table) {
sortedTable.Add Column (col.Name, col.Type); // something like this
}
foreach (DataRowView drv in dv) { // using dv from above
sortedTable.Add Row ( // get values from drv );
}

Neither of these solutions are ideal, but I can't think of any other "slick"
way of doing it.

--
Mike Mayer, C# MVP
mi**@mag37.com
http://www.mag37.com/csharp/

Nov 18 '05 #2
Your post went unanswered. Have you resolved this issue?

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"Fredrik Rodin" <ro***@dacsa.ne t> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
All,

I've been looking around for a solution to my problem for a couple of days
now.

In short, here's my situation:

1.
I'm getting a result from a component back as a datatable and I have no
option to sort the different columns during the call to the component.

Code-snippet:
Dim dTable As DataTable

Dim x As New MyComponent

'---between these two lines I add some parameters and properties
...x.blabla = ""
'---

2.
I then execute the component:

dTable = x.Execute()

So faar so good. No SQL invloved (only within the component).

Now comes the tricky part. I need to sort the result and display it.
However, I can't just use the .Sort in DataRow and get a 1-D array back. I
have properties in the component that I need to get back via an index. I've already tried to re-build it using the array but it's simply not working.

So I thought I could create a 1-D array and then copy it to a second
DataTable. Something like this:

dTable.Execute( )
Dim dRow As DataRow
dRow = dTable.Select(N OTHING, "COLUMN_NAM E ASC, COLUMN_NAME2 DESC")

I now have a 1D array that contains my sorted data from the DataTAble
Now I would like to create a new DataTable from the 1D array.

Does anybody have any idea how to solve it?

Thanks in advance,
Fredrik

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4320
by: Bill Todd | last post by:
I have a DataTable in a DataSet. A DataGrid is bound to the DataTable. When I assign a field to the DataTable.DefaultView.Sort property the order in the DataGrid does not change. What am I missing? -- Bill
6
9292
by: Hardy Wang | last post by:
Hi, I have a DataSet, returned by a stored procudure, which has only on DataTable. At this step there is no sort in DataTable. Is there is a way I can do to sort DataRows in this DataTable (not from select statement of sql query) in memory? Thanks -- WWW: http://hardywang.1accesshost.com ICQ: 3359839
1
2695
by: Gunjan Garg | last post by:
Hello All, I am working to create a generic datagrid which accepts a datasource(ListData - This is our own datatype) and depending on the calling program customizes itself for sorting, paginantion or accepting the add and remove item events. What i am observing is that none of the vents are happening... (Sort, page, or item). I am sure I am missing something basic here... Need help... Thanks much
5
2234
by: Gene Hubert | last post by:
I'm using the DefaultView from the Datasource for a DataGrid to present the data in a particular order. It seems that sorting in this way is an "Active Sort", as is the default sort that is provided by clicking on a column header. By "Active Sort", I mean that editing a value in a sorted column may cause the record to be moved to maintain the sort order. Is there an efficient way to present the data in a given order but not have it be...
18
1770
by: Doug Bell | last post by:
Hi, I have a function that loads Rows from a field in a DataTable into an ArrayList. See below: It also adds a Row (eg "All Areas"). I wanted this new row to be the first record so I added a preceeding space (eg " All Areas") and sorted the ArrayList. I have just found that there are some valid records that have one or more
8
2642
by: GrandpaB | last post by:
I need to sort an Arraylist. The Arraylist is contained in classArt. The Arraylist contains objects that have been defined in a sperate class, objArt. I have attempted to implement the sort using IComparable, but I'm hopelessly lost. With the addition of "Implements ICompariable" and the "CompareTo" function the program will ont even compile. Any suggestion that you have would be gratefully welcomed. I am including the relevant code: ...
1
1990
by: Lyners | last post by:
I am trying to figure out the best way to do this (currently I am having a problem sorting). I have a vb.net program that contains 2 datagrids on a form for the end user. When the user is ready to process, behind the scenes, I combine the 2 datagridas into a datatable, sort and do processing for creating a load into another system. My problem is that when I combine the two datagrids into the 1 datatable, I can only sort in a dataview. When...
4
1546
by: G .Net | last post by:
Hi I have a question which I hope you can help with. I am setting the DataSource of a DataGrid to be a DataView. I am sorting the DataView by various fields which include a Date. When I create a new row in the DataGrid and set the date to me within the current sort range, the newly created row "jumps" to its correct position in the grid. This obviously makes sense because it is following the sort rule.
3
5008
by: Bimal Kothari | last post by:
private DataTable SortDataTable(DataTable GetDataTable, string sort) { DataTable _NewDataTable = GetDataTable.Clone(); int rowCount = GetDataTable.Rows.Count; DataRow foundRows = GetDataTable.Select(null, sort); // Sort with Column name for (int i = 0; i < rowCount; i++) { object arr = new object;
0
9700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9564
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6841
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.