Sorting GridView?

G

George

I am trying to add sorting to GridView.

The columns become links, _Sorting event is called... but grid does not
"know" that it's sorted.
The protperties SortDirection; SortExpression are still blank and i can
not set them myself since they are read only. So next time i am clickining
on the column grid still calls Sorting event with SordDirection Ascending.

Here is a relevan code snippets

<asp:GridView AllowPaging="true" PageSize="5" AllowSorting=true ID="gv"
runat="server" AutoGenerateColumns="false" OnSorting="gv_Sorting" >
<Columns>
<asp:BoundField HeaderText="ID" DataField="Id"
SortExpression="Id" />
<asp:BoundField HeaderText="Name" DataField="Name"
SortExpression="Name" />
</Columns>
</asp:GridView>

protected void gv_Sorting(object sender, GridViewSortEventArgs e)
{
string sSort = e.SortExpression;
if (e.SortDirection == SortDirection.Ascending)
sSort += " DESC";
else
sSort += " ASC";
BindGrid(sSort);
}

void BindGrid(string sSort)
{
string sSql = "SELECT a.Id, a.Name, a.Available, a.Price, a.Sku,
a.Bin, a.Site FROM tblItems a ORDER BY " + sSort;
gv.DataSource = clsGlobal.GetData(sSql);
gv.DataBind();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top