Gridview正在排序,但无法使用递增的下降箭头添加Gridview ..如何添加?

时间:2017-04-24 18:43:46

标签: c# asp.net sorting gridview

这是我用箭头添加gridview的代码.Gridview按升序和降序排序,但我无法添加箭头。

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我错了,我已经采取了一个事件进行排序,这是我的排序gridview的代码它工作正常,但我无法添加箭头到girdview,上面我已经尝试过但它没有添加我箭,我怎么加箭头?

我已经尝试了一些文章,但是我无法添加带有升序和降序的箭头,gridview在点击标题行时进行排序,但需要向用户显示他可以根据升序箭头和降序箭头进行排序...

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

请帮忙??

1 个答案:

答案 0 :(得分:0)

您可以像这样添加图片。

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