Sorting Dataset

  • Thread starter Thread starter Chris Kennedy
  • Start date Start date
C

Chris Kennedy

How do you move rows up and down the order of the dataset and how do you
insert a row in a specific row and make all the rows after it go up one. Is
there an easy way of doing this or are we talking about something more
complicated like creating another dataset and reading one to the other and
designing an algorithm to do do the sorting. Regards, Chris.
 
Hi Chris,

It is very easy, however have also a look at the dataview.

A dataset is a container which holds tables that holds rows which has items.
(Actualy it are all references to seperate objects)

So when you see this

ds.tables(0).rows(0).item(0) 'VB notation with C# brackets

We are talking about the first item in the first row in the first table.

ds.tables(0).rows(ds.tables(0).rowcount-1).item(0)

is the last row in that same table.

I hope this clears if for you that you can go everywhere with that, because
of that the place of a row in a datatable is not really important, it is
just added to the end of the table.
(I know that you can say: "I can get it with an Order By sorted")

The dataview (a view on a table) is mostly used to sort it (an alternative
is the defaultview).

I hope this helps a little bit?

Cor
 
Sort of helps. Say I have rows 0, 1, 2, 3 and 4, and I want to insert into a
row into the second position e.g. 0, new row, 2,3,4 and 5. How do I get the
dataset to increment the row position of all the ones after the the new row.
In addition how do I simply move one up so when they are displayed in the
binded object differently. For example move row 1 to 2 and see all the other
rows increment and deincrement accordingly. The project I am working
requires the user be able to change the order in which the dataset is
displayed by moving items up and down. Regards, Chris.
 
Hi Chris,

When you use the dataview as the datasource, that is an automatic action.
You can add a datarow in very much ways to a dataset.

However the dataview will show it in the right order as you told in the sort
property.

I hope this helps?

Cor
 
Hi Chris:

You have kind of a unique problem and there are a few ways you can handle
this. You could create another column and at the onset, set each value to a
sequential numeric position. You could then sort on this column. If you
already have an account number or some other number you could make this
number the same so it sorted the same way. You could make this number in the
new row the same as the row above it. The dataview sorts automatically so
it'd add this next to the first number. You'd probably need some other
tweaks and no matter what it would probably be a bit ugly. If you want to do
something cool..

Basically what we want in an Insert Method for the Datatable's RowCollection
but we don't have one. However, the nice thing about bindings is that
anything that Implements IList can be used to bind the grid. So you could
create your own object, use the values from the rows in the object and bind
to your IList object. If you used an Array or an ArrayList or a whole slew
of other things (I belive CollectionBase has a Remove and RemoveAt) but this
gives you the ability to insert at a particular position, which is
essentially what you want. Furthermore ArrayLists for instance have a .Sort
method and although it may or may not give you what you want by default, you
can use a IComparer object to finely tune your sort.

The main point is that with a little work, you can do just about anything
you want most of the time. In this instance, implementing IList and
ICompare in a custom object or collection will allow you to not only bind a
grid to it, but create any type of custom sorting you may need. By using a
Arraylist or an array in your implementation you can take advantage of the
Insert method (or depending on what object you use InsertAt ) to specify a
position To be honest what you actually want is more on the lines of an
InsertAt but that's really not the point, I just wanted to illustrate that
with the implementation of a few interfaces, you can do a whole heck of a
lot.


Cheers,

Bill

--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
http://www.devbuzz.com/content/zinc_personal_media_center_pg1.asp
 
Back
Top