0

I would like to know a simple method to sort a column by date added , for example:

ID|Name|Age|Date 1 John 21 20/03/2012 2 Chart 22 21/03/2012 3 Dart 31 22/03/2012 4 Rat 12 23/03/2012 

So I would like to add a option to sort the column Date date of values , if you know what i mean.

THanks

6
  • A column of the SQL table? or a column of how you are storing the data in a variable, datatable, etc? Commented Mar 8, 2012 at 18:18
  • is 'Date' the same as 'Date Added'? What data structures are you using? Does this table live in SQL? What have you tried so far? Commented Mar 8, 2012 at 18:18
  • Which technology are you using to pull data from your database? Do you want to sort it at the source or sort it in the UI? Which technology are you using to display your data? Commented Mar 8, 2012 at 18:18
  • Yes Sql database , sort it at the UI , I'm using gridview sql data source to display the values Commented Mar 8, 2012 at 18:21
  • How are you getting the info back from SQL? Linq2Something, a direct text SQL query, etc? Commented Mar 8, 2012 at 18:26

2 Answers 2

5

Are you trying to use Linq2Sql? If so, you can just do:

var result = from x in MyDataTable orderby x.Date ascending select x; 

(replace ascending by descending if you want it to go the other way).

If you would like to optionally sort it, you can use IQueryable<T>s and optionally sort, a la:

IQueryable<MyDataTable> GetData(bool sortByDate) { var result = from x in DataContext.Table<MyDataTable> where SomeMatchingClause(x) select x; if(sortByDate) { result = result.OrderBy(x => x.Date); // or OrderByDescending } return result; } 

If you're querying in SQL directly, just do

SELECT * FROM MyTable ORDER BY Date ASC 

(replace ASC with DESC if you want it to go the other way)

I don't frequently build SQL queries as strings in C#, but if you wanted to make this optional, I guess you could incrementally build the query, even though it's really gross:

string GetSqlQuery(bool sortByDate) { string result = "SELECT * FROM MyTable"; if(sortByDate) { result += " ORDER BY Date ASC"; } return result; } 
Sign up to request clarification or add additional context in comments.

5 Comments

I used on a button SELECT * FROM MyTable ORDER BY Date ASC , sqlconnect,sqlcommand with the string select and execute scalar. nothing happened
Did you replace MyTable with the actual name of your table? I'm fairly unfamiliar with SqlConnect and SqlCommand, but it seems like ExecuteScalar may not be the correct method to bring back a set of tabular data. Looking at this (msdn.microsoft.com/en-us/library/…), it seems like maybe ExecuteReader is what you need to do?
yes , nothing happened , the rows are in the same position . if there's another simple method please explain me how to do it . thank you very much
This is how you order rows in SQL. It looks like you showed a table whose Dates are presorted: change ASC to DESC and it should show up in the opposite order.
nothing happened , the rows are in the same position , they need to be arranged by date from Date column
0

In your SQL query, put order by DATE at the end

2 Comments

Yes Sql database , sort it at the UI , I'm using gridview sql data source to display the values
If you have Linq working use eouw0o83hf's answer posted above. Linq lets you sort the data after it comes from the DB meaning you don't have to change your SQL code. If you don't have Linq, you can change your SQL code to sort the data. Hope this helps.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.