Paul Kahl
asked on
Bi-Directional Sorting in Gridview
Given the following Grid (and c# code), how do I set it up to sort Ascending AND Descending on Header-Click? (The idea is to click the header once to sort in one direction, and upon the grid reload, to set the sort direction to go the other way on the next click).
ASPX page:
<asp:GridView ID="grdvwActivityStmt" CssClass="idGrid" CellPadding="3"
AutoGenerateColumns="false " runat="server" Width="100%"
EmptyDataText="No Activity Statements on file."
AllowPaging="true" AllowSorting="true"
PageSize="100" OnPageIndexChanging="grdvw ActivitySt mt_PageInd exChanging "
OnSorting ="grdvwActivityStmt_OnSort ing" DataKeyNames="SSN" GridLines="None">
<HeaderStyle CssClass="ItemHeader" />
<RowStyle CssClass="Item" />
<AlternatingRowStyle CssClass="AltItem" />
<PagerSettings FirstPageText="<< First   " LastPageText="    Last >>" PreviousPageText=" &l t; Prev " NextPageText=" Next > " Mode="NextPreviousFirstLas t" Position="TopAndBottom" />
<PagerStyle Font-Bold="True" BackColor="#FFFFFF" CssClass="BlackBold" HorizontalAlign="Center" BorderStyle="None" />
<Columns>
<asp:templatefield headertext="ST" SortExpression="Destinatio nState">
<ItemStyle HorizontalAlign="Center" Width="10%" />
<HeaderStyle HorizontalAlign="Center" />
<itemtemplate>
<%# DataBinder.Eval(Container. DataItem, "DestinationState") %>
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="Acknowledgemen t" SortExpression="HistoryCre ateDate">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Width="30%" />
<itemtemplate>
<%# DataBinder.Eval(Container. DataItem, "HistoryCreateDate") %>
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="EFIN" SortExpression="EFIN">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" Width="15%" />
<itemtemplate>
<%# DataBinder.Eval(Container. DataItem, "EFIN") %>
</itemtemplate>
</asp:templatefield>
</Columns>
</asp:GridView>
ASPX.CS page:
DataSet ds = new DataSet();
DataView dv = new DataView();
#region Page Load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dv = bindgrid(intAxaptaCustNum, strCustGUID);
grdvwPgOne.DataSource = dv;
grdvwPgOne.DataBind();
}
}
#endregion
#region Database Output Functions
/// <summary>
/// Binding for the main gridView
/// </summary>
/// <returns></returns>
private DataView bindgrid(string strAcctNum, string strCustGUID)
{
SqlConnection SQLConn = new SqlConnection(FCTaxContent .FCTaxCont entConnect ionString( ));
SqlCommand sqlCmd = new SqlCommand("spx_GetInvoice DataPageOn e", SQLConn);
sqlCmd.CommandType = CommandType.StoredProcedur e;
SqlDataAdapter sqlAdp = new SqlDataAdapter(sqlCmd);
sqlCmd.Parameters.Add("@Ac ctNum", SqlDbType.VarChar);
sqlCmd.Parameters["@AcctNu m"].Value = strAcctNum;
sqlCmd.Parameters.Add("@Cu stGUID", SqlDbType.VarChar);
sqlCmd.Parameters["@CustGU ID"].Value = strCustGUID;
sqlAdp.Fill(ds);
if (ViewState["sortExpr"] != null)
{
dv = new DataView(ds.Tables[0]);
dv.Sort = (string)ViewState["sortExp r"];
}
else
{
dv = ds.Tables[0].DefaultView;
}
pnlActivityOutput.Visible = true;
return dv;
}
/// <summary>
/// Sorting for the Header clicks in the main gridView
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void grdvwPgOne_OnSorting(Objec t sender, GridViewSortEventArgs e)
{
if (e.SortDirection == SortDirection.Ascending)
{
ViewState["sortExpr"] = e.SortExpression;
}
else
{
ViewState["sortExpr"] = string.Concat(e.SortExpres sion, " ", SortDirection.Descending);
}
grdvwPgOne.DataSource = bindgrid(Session["AxaptaCu stomerNumb er"].ToStr ing(), Session["AxaptaCustGUID"]. ToString() );
grdvwPgOne.DataBind();
pnlActivityOutput.Visible = true;
}
/// <summary>
/// Paging handler for the main gridView
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void grdvwPgOne_PageIndexChangi ng(Object sender, GridViewPageEventArgs e)
{
grdvwPgOne.PageIndex = e.NewPageIndex;
grdvwPgOne.DataSource = bindgrid(Session["AxaptaCu stomerNumb er"].ToStr ing(), Session["AxaptaCustGUID"]. ToString() );
grdvwPgOne.DataBind();
pnlActivityOutput.Visible = true;
}
#endregion
I'd give more points if there was a way, as this one has stumped me for quite some time.
ASPX page:
<asp:GridView ID="grdvwActivityStmt" CssClass="idGrid" CellPadding="3"
AutoGenerateColumns="false
EmptyDataText="No Activity Statements on file."
AllowPaging="true" AllowSorting="true"
PageSize="100" OnPageIndexChanging="grdvw
OnSorting ="grdvwActivityStmt_OnSort
<HeaderStyle CssClass="ItemHeader" />
<RowStyle CssClass="Item" />
<AlternatingRowStyle CssClass="AltItem" />
<PagerSettings FirstPageText="<< First   " LastPageText="  
<PagerStyle Font-Bold="True" BackColor="#FFFFFF" CssClass="BlackBold" HorizontalAlign="Center" BorderStyle="None" />
<Columns>
<asp:templatefield headertext="ST" SortExpression="Destinatio
<ItemStyle HorizontalAlign="Center" Width="10%" />
<HeaderStyle HorizontalAlign="Center" />
<itemtemplate>
<%# DataBinder.Eval(Container.
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="Acknowledgemen
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Width="30%" />
<itemtemplate>
<%# DataBinder.Eval(Container.
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="EFIN" SortExpression="EFIN">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" Width="15%" />
<itemtemplate>
<%# DataBinder.Eval(Container.
</itemtemplate>
</asp:templatefield>
</Columns>
</asp:GridView>
ASPX.CS page:
DataSet ds = new DataSet();
DataView dv = new DataView();
#region Page Load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dv = bindgrid(intAxaptaCustNum,
grdvwPgOne.DataSource = dv;
grdvwPgOne.DataBind();
}
}
#endregion
#region Database Output Functions
/// <summary>
/// Binding for the main gridView
/// </summary>
/// <returns></returns>
private DataView bindgrid(string strAcctNum, string strCustGUID)
{
SqlConnection SQLConn = new SqlConnection(FCTaxContent
SqlCommand sqlCmd = new SqlCommand("spx_GetInvoice
sqlCmd.CommandType = CommandType.StoredProcedur
SqlDataAdapter sqlAdp = new SqlDataAdapter(sqlCmd);
sqlCmd.Parameters.Add("@Ac
sqlCmd.Parameters["@AcctNu
sqlCmd.Parameters.Add("@Cu
sqlCmd.Parameters["@CustGU
sqlAdp.Fill(ds);
if (ViewState["sortExpr"] != null)
{
dv = new DataView(ds.Tables[0]);
dv.Sort = (string)ViewState["sortExp
}
else
{
dv = ds.Tables[0].DefaultView;
}
pnlActivityOutput.Visible = true;
return dv;
}
/// <summary>
/// Sorting for the Header clicks in the main gridView
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void grdvwPgOne_OnSorting(Objec
{
if (e.SortDirection == SortDirection.Ascending)
{
ViewState["sortExpr"] = e.SortExpression;
}
else
{
ViewState["sortExpr"] = string.Concat(e.SortExpres
}
grdvwPgOne.DataSource = bindgrid(Session["AxaptaCu
grdvwPgOne.DataBind();
pnlActivityOutput.Visible = true;
}
/// <summary>
/// Paging handler for the main gridView
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void grdvwPgOne_PageIndexChangi
{
grdvwPgOne.PageIndex = e.NewPageIndex;
grdvwPgOne.DataSource = bindgrid(Session["AxaptaCu
grdvwPgOne.DataBind();
pnlActivityOutput.Visible = true;
}
#endregion
I'd give more points if there was a way, as this one has stumped me for quite some time.
ASKER
Store it how? Session based? The idea is NOT to pass it through the query string. I want to prevent users (or abusers, really) from hacking urls and doing what they want, so I'd like to keep it in code.
Yeah.. i admit I was thinking session based....
Another option would be to create your own derived DataGrid class and allow sorting logic to be handled with the object itself..
benefits would include not having to rely on outside source of control or information to determine control behavior.. ie session variable and or parameter and redundantly, cleaner implementation with the exact functionality you are wanting.
Another option would be to create your own derived DataGrid class and allow sorting logic to be handled with the object itself..
benefits would include not having to rely on outside source of control or information to determine control behavior.. ie session variable and or parameter and redundantly, cleaner implementation with the exact functionality you are wanting.
ASKER
That's already what I'm doing, actually. I have a built-in sort functionality in the grdvwPgOne_OnSorting function, but I can't get it to switch directions.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
By jove, I think you've got it!
Here's the finished version, with detection for the object of session content.
protected void grdvwPgOne_OnSorting(Objec t sender, GridViewSortEventArgs e)
{
object objSortDir = Session["LastSortDirection "];
if (!(objSortDir is object))
{
Session["LastSortDirection "] = "asc";
ViewState["sortExpr"] = e.SortExpression;
}
else
{
if (Session["LastSortDirectio n"].ToStri ng() == "asc")
{
ViewState["sortExpr"] = string.Concat(e.SortExpres sion, " desc");
Session["LastSortDirection "] = "desc";
}
else
{
Session["LastSortDirection "] = "asc";
ViewState["sortExpr"] = e.SortExpression;
}
}
grdvwPgOne.DataSource = bindgrid(Session["AxaptaCu stomerNumb er"].ToStr ing(), Session["AxaptaCustGUID"]. ToString() );
grdvwPgOne.DataBind();
pnlPg1.Visible = true;
}
Here's the finished version, with detection for the object of session content.
protected void grdvwPgOne_OnSorting(Objec
{
object objSortDir = Session["LastSortDirection
if (!(objSortDir is object))
{
Session["LastSortDirection
ViewState["sortExpr"] = e.SortExpression;
}
else
{
if (Session["LastSortDirectio
{
ViewState["sortExpr"] = string.Concat(e.SortExpres
Session["LastSortDirection
}
else
{
Session["LastSortDirection
ViewState["sortExpr"] = e.SortExpression;
}
}
grdvwPgOne.DataSource = bindgrid(Session["AxaptaCu
grdvwPgOne.DataBind();
pnlPg1.Visible = true;
}
Cool!
Why not store which direction the grid was sorted by last time and do the opposite direction next time the user asks for a sort?