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

n8wrl
  • 19,439
  • 4
  • 63
  • 103
Inzi Irina
  • 267
  • 2
  • 5
  • 14
  • A column of the SQL table? or a column of how you are storing the data in a variable, datatable, etc? – Eric H Mar 08 '12 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? – n8wrl Mar 08 '12 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? – Cᴏʀʏ Mar 08 '12 at 18:18
  • Yes Sql database , sort it at the UI , I'm using gridview sql data source to display the values – Inzi Irina Mar 08 '12 at 18:21
  • How are you getting the info back from SQL? Linq2Something, a direct text SQL query, etc? – eouw0o83hf Mar 08 '12 at 18:26
  • I think you're trying to sort the gridview columns. Here's a similar post http://stackoverflow.com/questions/138412/how-to-sort-columns-in-an-asp-net-gridview-if-using-a-custom-datasource – Divi Mar 09 '12 at 02:01

2 Answers2

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;
}
eouw0o83hf
  • 9,438
  • 5
  • 53
  • 75
  • I used on a button SELECT * FROM MyTable ORDER BY Date ASC , sqlconnect,sqlcommand with the string select and execute scalar. nothing happened – Inzi Irina Mar 08 '12 at 19:38
  • 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 (http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx), it seems like maybe `ExecuteReader` is what you need to do? – eouw0o83hf Mar 08 '12 at 19:45
  • 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 – Inzi Irina Mar 08 '12 at 19:50
  • This is how you order rows in SQL. It looks like you showed a table whose `Date`s are presorted: change `ASC` to `DESC` and it should show up in the opposite order. – eouw0o83hf Mar 08 '12 at 19:57
  • nothing happened , the rows are in the same position , they need to be arranged by date from Date column – Inzi Irina Mar 08 '12 at 19:59
0

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

C0D3
  • 6,440
  • 8
  • 43
  • 67
  • Yes Sql database , sort it at the UI , I'm using gridview sql data source to display the values – Inzi Irina Mar 08 '12 at 18:20
  • 1
    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. – C0D3 Mar 08 '12 at 18:25