ASP.NET - erroe occured while sorting GridView

Asked By aman on 03-Aug-11 07:07 AM
hi all,
the below error occured while sorting Gridview

The GridView 'GridView1' fired event Sorting which wasn't handled.

please help me with some example of sorting GridView.
Jitendra Faye replied to aman on 03-Aug-11 07:09 AM
That's because you haven't wired up the paging and sorting events...

AllowPaging="True" AllowSorting="true" pagesize="3"
OnPageIndexChanging="Gridview1_PageIndexChanging"
OnSorting="Gridview1_Sorting"
/>

protected void Gridview1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
// do something
}

protected void Gridview1_Sorting(object sender, GridViewSortEventArgs e)
{
// do something
}

Ravi S replied to aman on 03-Aug-11 07:09 AM
HI

I think,That's because you haven't wired up the paging and sorting events...

AllowPaging="True" AllowSorting="true" pagesize="3"
OnPageIndexChanging="Gridview1_PageIndexChanging"
OnSorting="Gridview1_Sorting"
/>

protected void Gridview1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
// do something
}

protected void Gridview1_Sorting(object sender, GridViewSortEventArgs e)
{
// do something
}
Ravi S replied to aman on 03-Aug-11 07:10 AM
Hi

That's because you haven't wired up the paging and sorting events...

<asp:GridView runat="server" id="Gridview1"
AllowPaging="True" AllowSorting="true" pagesize="3"
OnPageIndexChanging="Gridview1_PageIndexChanging"
OnSorting="Gridview1_Sorting"
/>


protected void Gridview1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
// do something
}

protected void Gridview1_Sorting(object sender, GridViewSortEventArgs e)
{
// do something
}
dipa ahuja replied to aman on 03-Aug-11 07:16 AM
The error is coming because you are trying to fire the sorting event, but you have not implemented the event: Follow this steps to sort the gridview:

   protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      ViewState["sortOrder"] = "";
      bindGridView("""");
    }
  }
  public void bindGridView(string sortExp, string sortDir)
  {
    string ConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection SQLconnection = new SqlConnection(ConnString);
 
    if (SQLconnection.State == ConnectionState.Closed)
    {
      SQLconnection.Open();
    }
 
    SqlCommand mySqlCommand = new SqlCommand("select * from people", SQLconnection);
    SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);
    DataSet myDataSet = new DataSet();
    mySqlAdapter.Fill(myDataSet);
 
    DataView myDataView = new DataView();
    myDataView = myDataSet.Tables[0].DefaultView;
 
    if (sortExp != string.Empty)
    {
      myDataView.Sort = string.Format("{0} {1}", sortExp, sortDir);
    }
 
    GridView1.DataSource = myDataView;
    GridView1.DataBind();
 
    // if condition that can be used to check the sql connection
    // if it is open then close it.
    if (SQLconnection.State == ConnectionState.Open)
    {
      SQLconnection.Close();
    }
  }
 
  protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
  {
    bindGridView(e.SortExpression, sortOrder);
  }
  public string sortOrder
  {
    get
    {
      if (ViewState["sortOrder"].ToString() == "desc")
      {
        ViewState["sortOrder"] = "asc";
      }
      else
      {
        ViewState["sortOrder"] = "desc";
      }
 
      return ViewState["sortOrder"].ToString();
    }
    set
    {
      ViewState["sortOrder"] = value;
    }
  }