简体   繁体   中英

Gridview is sorting but Unable to add Gridview with ascending descending arrows..How to add?

This is my code for adding gridview with arrows..Gridview is sorting by ascending and descending but i am unable to add arrows.

protected void grdInformation_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row != null && e.Row.RowType == DataControlRowType.Header)
    {
        foreach (TableCell cell in e.Row.Cells)
        {
            if (cell.HasControls())
            {
                LinkButton button = cell.Controls[0] as LinkButton;
                HtmlGenericControl gv = new HtmlGenericControl("div");

                Label lnkName = new Label();
                lnkName.Text = button.Text;

                if (button != null)
                {
                    Image imageSort = new Image();
                    imageSort.ImageUrl = "~/images/asc.png";

                    if (grdInformation.SortExpression == button.CommandArgument)
                    {
                        if (grdInformation.SortDirection == SortDirection.Ascending)
                        {
                            imageSort.ImageUrl = "~/images/desc.png";
                        }
                        else
                        {
                            imageSort.ImageUrl = "~/images/asc.png";
                        }
                    }

                    gv.Controls.Add(lnkName);
                    gv.Controls.Add(imageSort);
                    button.Controls.Add(gv);
                    }
                }
            }
        }
    }
}}

Dono where i am going wrong , I have took an event for sorting , this is my code for sorting gridview it is working finee, but i am unable to add arrows to the girdview , the above i have tried but its not adding me arrows, how do i add arrows ??

i have tried some articles but i am unable to add arrows with ascending and descending , gridview is sorting when clicking on header rows , but need to display to the user that he can sort based on ascending arrows and descending arrows ...

 protected void grdInformation_Sorting(object sender, GridViewSortEventArgs e)
 {
     if (CurrentSortExpression == e.SortExpression.ToString())
     {
         if (CurrentSortDirection == "asc")
             CurrentSortDirection = "desc";
         else
             CurrentSortDirection = "asc";
     }
     else
     {
         CurrentSortExpression = e.SortExpression.ToString();
         CurrentSortDirection = "asc";
     }

     if (e.SortExpression.Trim() == this.SortField)
     {
         this.sortDirection = (this.sortDirection == "DESC" ? "ASC" : "DESC");
      }
      else
      {
          this.sortDirection = "ASC";
      }

      ViewState["SortDirection"] = this.sortDirection;
      this.SortField = e.SortExpression;

      Bindinfo(GetInformation(ddlStatus.SelectedValue, ddlGroups.SelectedValue));
}

Please help ??

You can add the images like this.

System.Web.UI.WebControls.Image sortArrow = new System.Web.UI.WebControls.Image();

private void addSortImages()
{
    int columnIndex = 0;

    //set the image url
    sortArrow.ImageUrl = "~/images/asc.png";
    if (mySortDirection == SortDirection.Descending)
    {
        sortArrow.ImageUrl = "~/images/desc.png";
    }

    //check for rows in the gridview
    if (GridView1.Rows.Count > 0)
    {
        //loop all the columns
        foreach (DataControlFieldHeaderCell cell in GridView1.HeaderRow.Cells)
        {
            if (cell.ContainingField.SortExpression == mySortExpression)
            {
                columnIndex = GridView1.HeaderRow.Cells.GetCellIndex(cell);
            }
        }

        //add the image to the correct header cell
        GridView1.HeaderRow.Cells[columnIndex].Controls.Add(sortArrow);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM