Link to home
Start Free TrialLog in
Avatar of patgarvey
patgarvey

asked on

C# Datagrid can't get sort command and item command to both work


I built a datagrid "TireDisplay", and I added this cool two-way sorting function:

void TireDisplayGrid_SortCommand(Object sender, DataGridSortCommandEventArgs e)
{
       string sortDir = "";
       if (Request.QueryString["sortOn"] == e.SortExpression)
             {
           // The sort expression matches what was sorted on last time
                       if (Request.QueryString["sortDir"] == "")
                 {
                 sortDir = "ASC";
                 }
           else if (Request.QueryString["sortDir"] == "ASC")
                 {
                 sortDir = "DESC";
                 }
           else if (Request.QueryString["sortDir"] == "DESC")
                 {
                 sortDir = "ASC";
                 }
             }
       else
       {
      // Have a new sort expression so always start out ASC
      sortDir = "ASC";
       }
     
 // Redirect back to this page with the appropriate sort expression and direction.
 Response.Redirect(Request.ServerVariables["SCRIPT_NAME"] + "?sortOn=" + e.SortExpression + "&sortDir=" + sortDir);
}

Everything works great.  Next, I needed a detail link for items in the grid.  So I added an event handler for a button column:

void GetQuote_ItemCommand(Object sender, DataGridCommandEventArgs e)
{
//get values from row where button is clicked, create a session variable, and go to detail page
string SelectedPrice = e.Item.Cells[5].Text;
Session["QuotePrice"] = SelectedPrice;
Response.Redirect("PriceQuote.aspx");
}

Everthing still works.  Then I added the button column:

<asp:ButtonColumn Text="Get a Quote" ButtonType="LinkButton" Visible="true"/>

Still OK!  But the final piece is the "OnItemCommand = GetQuote_ItemCommand" attribute of the grid.  As soon as I add this, the sort command breaks.  It now tries to activate the GetQuote_ItemCommand event, and the app breaks.

It seems like it can't distinguish between DataGridCommandEventArgs  and DataGridSortCommandEventArgs

Help!!



ASKER CERTIFIED SOLUTION
Avatar of tusharashah
tusharashah

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of patgarvey
patgarvey

ASKER


That worked!!  Thanks tushar!!