Datareader Sorting

Results 1 to 2 of 2

Thread: Datareader Sorting

  1. #1
    Join Date
    Dec 1969

    Default Datareader Sorting

    I have a datareader that I want to sort as I bind it to my Datagrid. With a dataset, I would just use the command:<BR><BR>MyDs.Tables(0).DefaultView.Sort = sQueryOrder<BR><BR>Where sQueryOrder had my sorting logic.<BR><BR>I can&#039;t find a similar property for a datareader. I tried to pass my sort string into the function that creates my datareader, but I ran into another problem. If I set-up a query that has some complicated logic like:<BR><BR>Select [Complicated Query Logic Here] as SimpleFieldName<BR><BR>and then I try to add the sorting logic at the end of the query:<BR><BR>Order by SimpleFieldName<BR><BR>It gives me an error that it doesn&#039;t know what SimpleFieldName is and I have to reconstruct [Complicated Query Logic Here] which is very taxing.<BR><BR>Any suggestions on how to easily handle sorting when binding a datareader to a datagrid? As it is now, I&#039;m going to have to sacrifice my performance gains with the Datareader and use a Dataset instead.<BR><BR>Please advise.<BR><BR>Thanks,<BR>Jason

  2. #2
    Join Date
    Dec 1969

    Default I gather this is with an Access DB...

    ...because your use of<BR> ORDER BY SimpleFieldName<BR>*should* work with SQL Server.<BR><BR>But with either DB, there&#039;s an easy answer:<BR> ORDER BY 1<BR><BR>Yes, you can use the field *NUMBER*, where the fields *IN THE SELECT* are numbered starting at ONE. (Why one, when everything else starts at zero? Who knows!)<BR><BR>So if you do<BR><BR>SELECT [complicated logic] As someName, [other logic] As otherName,,<BR>FROM A, B<BR>WHERE ...<BR>ORDER BY 2 DESC, 1 ASC<BR><BR>it&#039;s the same as doing<BR><BR>ORDER BY otherName DESC, someName ASC<BR><BR>I generally put my complicated logic expressions *FIRST* in the SELECT list, so that indeed I can use<BR> ORDER BY 1<BR>and don&#039;t have to worry about getting the count correct.<BR><BR>IMPORTANT NOTE: Unfortunately, this capability does *NOT* apply to GROUP BY clauses! If you need that same field in a GROUP BY, you have no choice but to copy/paste the expression in place. Don&#039;t worry, though: The query engine is smart enough to realize that the expressions are the same and it won&#039;t do the calculation twice. Even if using Access.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts