Reverse a Dataset

  • Thread starter Thread starter Paez
  • Start date Start date
P

Paez

Hi there.

Anybody knows if is it possible to reverse a dataset? Lets say, if the
dataSet has 10 rows, row 1 will row 10 and row 10 will be 1 and so on...

Thanks in advance.

Paez
 
Anybody knows if is it possible to reverse a dataset? Lets say, if the
dataSet has 10 rows, row 1 will row 10 and row 10 will be 1 and so on...

I don't think there's anything built-in which would do this...

However, you could certainly walk through the rows in reverse order:

for (int intRow = MyDataSet.Tables[0].Rows.Count - 1; intRow >= 0; intRow--)
{
// do something with MyDataSet.Tables[0].Rows[intRow]
}
 
Hi Paez,

If you need to sort the DataRows just to display them on a DataGridView via
binding, for example, then just bind to a DataView instead and set the Sort
property. It's much easier and certainly more practical then reordering the
actual DataRows within the DataTable.

// dataTable is an instance of your DataTable
dataTable.DefaultView.Sort = "FirstName DESC";

dataGridView.DataSource = dataTable.DefaultView;

DataView Class
http://msdn2.microsoft.com/en-us/library/system.data.dataview.aspx

You'll need a column to sort on, of course. If you simply need to reverse
the records as they exist in the DataTable then add a new AutoNumber column
to the DataTable and sort by that column.

Data Access in Client and Middle-Tier Programming
How to: Create an Autonumber DataColumn
http://msdn2.microsoft.com/en-gb/library/yctw654b(VS.80).aspx
 
Hi,

Most probably you mean a DataTable, a dataset is mainly a set of Datatables
that are the one that contain the data.

There is nothing like that I'm afraid, the internal structure to hold the
DataRow is "unknow" how it store the elements.

What do you want to do anyway?

You can use a DataView and sort the rows in the order you want pretty
easily.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top