473,320 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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(NOTHING, "COLUMN_NAME 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 3152
"Fredrik Rodin" <ro***@dacsa.net> wrote in message
news:%2****************@TK2MSFTNGP09.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.AddColumn (col.Name, col.Type); // something like this
}
foreach (DataRowView drv in dv) { // using dv from above
sortedTable.AddRow ( // 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.net> wrote in message
news:%2****************@TK2MSFTNGP09.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(NOTHING, "COLUMN_NAME 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
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...
6
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...
1
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,...
5
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...
18
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...
8
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...
1
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...
4
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...
3
by: Bimal Kothari | last post by:
private DataTable SortDataTable(DataTable GetDataTable, string sort) { DataTable _NewDataTable = GetDataTable.Clone(); int rowCount = GetDataTable.Rows.Count; DataRow foundRows =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.