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();
}
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();
}