Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Gridview正在排序,但无法添加带有升降箭头的Gridview。如何添加?_C#_Asp.net_Sorting_Gridview - Fatal编程技术网

C# Gridview正在排序,但无法添加带有升降箭头的Gridview。如何添加?

C# Gridview正在排序,但无法添加带有升降箭头的Gridview。如何添加?,c#,asp.net,sorting,gridview,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.R

这是我添加带箭头的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));
}

请帮助???

您可以像这样添加图像

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