Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi all,

 

I'm working in a project that uses DataSets and I found a trouble shotting trying to set up in ordering a column of a table contained in the data set, in order to achive I use DataViews, this DataView works perfecly, then I retun the ordered data to the DataSet and then retun this DataSet as WebMethod but the data does simple not work, do anyone know what is the problem??, here is an code of my development...

 

tnks

 

public DataSet MyMethod(DataSet myDS)

{

// this data view is working perfecly

DataView myDView = new DataView(myDS.Tables[tablename], "", <column ASC>, DataViewRowState.CurrentRows);

 

// I'm doing this remove because if doesn't it causes assignment problems

myDS.Tables.Remove(tablename);

// I'm trying to assign the ordered data to the DataSet

myDS.Tables.Add(myDView.Table);

 

return myDS;

}

 

// in this web method does not work properly

[WebMethod]

public DataSet Test()

{

...

DataSet DS = MyMethod(myDS)

return DS;

}

Posted
The DataView is a customizable view of a DataSet. It doesn't actually contains a copy of the DataSet itself, it only references it. Therefore, when you sort a DataView you don't actually sort the underlying DataSet, only the way the DataView references it. So trying to add a table from the DataView back into a DataSet isn't going to work.
  • *Experts*
Posted

To add to Wjousts's comment, if you want a sorted view on the client, they'll have to load the DS into a DataView and sort it. If it's for binding, you can bind to the sorted DataView.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

DataSet

 

Ok Wjousts

 

I got your point of view, but, anyway, could be any way to resolve this?, I mean, having a DataSet in a method class and return it but sorted by any column?? by any other class or something?? Nerseus, do you have any idea about my question??

 

thank you for all

  • *Experts*
Posted

Other than sorting in the Database itself (add an ORDER BY clause to your SQL), I don't think the DataSet will sort internally - at least, I've never seen a method to do it. You might be able to manually loop and save off data rows and add them back in sorted, but I don't know that there's any guarantee that they'll be sorted in the DataTable.

 

I think your best bet is to use a DataView whenever you need a sorted view of your data. The DataView is perfect for binding, and you don't have to create a new one. Every DataTable has a DefaultView property which is a DataView associated to the table. You can directly change it's sorting - but to see it, you STILL have to go through the DefaultView property (for binding, for instance).

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

I'd have to agree with Nerseus, resorting the whole database itself could be very expensive computationally speaking, so ask yourself if you really, really need it to be sorted. If you just want to present it to the user as sorted, then use a dataview which I believe (and I'm not really 100% about how the dataview works) will sort without the overhead of actually swapping the rows of data by swapping the references to the rows of data in the dataview object.

Alternatively you can add a ORDER BY clause to your SQL statement and the dataset itself will be ordered (but the database will still be the same). I guess if you wanted to actually sort the database itself, you could retrive the sorted dataset by adding an ORDER BY clause in your SQL, delete EVERY row from your database (by running multiple DELETE SQL statements) and then INSERT every row from your dataset back into the database, but that is likely to be slow for any non-trivial database.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...