- There’s a gridview on my asp page and sqldatasource to bind the data
table info to gridview. - I have 6 columns:
- First column is called ID : type : int, increment by one , primary
key. - The other columns are name,age etc..
- The last column is called Added : type : datetime .
I need to add a button or dropdownlist doesn’t matter where I can sort the entire gridview rows by the DateTime Column.
Since you’re using an
SQLDataSourcewith theGridviewalready, I would say using the AllowSorting property of the GridView (AllowSorting="True"in your markup) would be your easiest bet. This will auto-generate buttons that let you sort by any column you like.For example:
A second option would be to do the sorting in your datasource. You could just change the
SelectCommandin yourSQLDataSourceto end with “ORDER BY Added DESC.” This would be ideal if you do not want the sorting to be a dynamic, user-prompted event, but just a static, default sorting of theGridViewthat does not change.For example: