Why is my Datagrid Sort not working?

Why is my Datagrid Sort not working?

Post by anoniek » Wed, 09 Feb 2005 05:17:58


This applies to ASPNET 1.1 and it took me a bit of time figuring
out cause of the problem

I enabled sorting. I have a method that
handles the SortCommand and it fires ok.

After getting the view and assigning the field "Sort"
of the dataview with the correct expression,
the datagrid still doesn't sort.

Well, here is one reason. Look the the HTML of the datagrid.

If the "DataSource" attribute is defined, then you have to
remove it. For example, the following has the attribute "DataSource"

<asp:datagrid id=TransactionsDataGrid runat="server"
AllowSorting="True" AutoGenerateColumns="False" AllowPaging="True"
CellPadding="2" BackColor="White" BorderWidth="1px" BorderStyle="None"
BorderColor="#3366CC" Width="100%" ShowFooter="True" DataSource="<%#
transactionsDataSet %>">

After removing that attribute, try to see if it would now work:

Here are some snippets: (for reference)

private void DataGrid1_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
Session["PrevSort"] = Session["Sort"];
Session["Sort"] = (string) e.SortExpression;

// Logic to reverse the sort when same link is clicked again.
string prevSort = (string) Session["PrevSort"];
string currSort = (string) Session["Sort"];

if ( prevSort.Replace("DESC", "").Trim() ==
currSort.Replace("DESC", "").Trim() )
if ( prevSort.EndsWith("DESC") )
currSort.Replace("DESC", "");
currSort += " DESC";
Session["Sort"] = currSort;


private DataView CreateDataSource()
sqlConnection1.ConnectionString = GetConnectionString();
DataView dv = transactionsDataSet.Tables[0].DefaultView;
string sortKey = (string) Session["Sort"];

dv.Sort = sortKey;
return dv;

private void BindGrid()
// Assign datasource only here. Remove any assignment to
attribute 'DataSource' in the HTML
TransactionsDataGrid.DataSource = CreateDataSource();

// This will attemp to Bind. If pageindex is out of synch, it
resets to first page.
catch( HttpException exc)
if ( exc.Message.StartsWith("Invalid CurrentPageIndex
value") )
TransactionsDataGrid.CurrentPageIndex = 0;

private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
TransactionsDataGrid.CurrentPageIndex = e.NewPageIndex;

1. Why i am not able to sort the amount columns having label function in a datagrid?

2. Web serice works when I am debugging, but does not when I am not.

I have a C#.net Web application which calls a web service
( http://www.yqcomputer.com/ ). It has run
just fine for months. Recently I started getting "System.Net.WebException:
The underlying connection was closed" errors. I started the application up in
debugging mode and it ran fine. After fiddling with things for awhile I now
find that it works while I am running in debug mode, but does not when I am
not in debug mode. I don't know if it matters, but I am not calling the web
service asynchronously.

Any idea what is going on? I am going to try it asynchronously and see if
that works, but this seems really weird to me either way!

3. Sorting Datagrid with a Select Column - Does not work!

4. i am not supposed to be on network so why is my PC saying I am?

5. ASP.NET DataGrid Sorting/Paging... what am I missing?

6. i am not supposed to be on network so why is my PC saying I am

7. WML:why calendar reminders only work if I am not connected to Live

8. why can i not open files in xls format am running microsoft work

9. Why does this cell renderer work for a listbox, but not for a datagrid?

10. Why is my Paging not working in my DataGrid?

11. Desperate - Gridview each row different from DataGrid - why not works?

12. Why does depth sorting not work in my code?

13. why does Random: Rnd() not work for random sort?

14. why fourier transform in AM and FM why not LT

15. OY: why fourier transform in AM and FM why not LT