【问题标题】:Gridview is sorting but Unable to add Gridview with ascending descending arrows..How to add?Gridview 正在排序,但无法添加带有升序降序箭头的 Gridview..如何添加?
【发布时间】:2017-09-21 13:46:36
【问题描述】:

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

请帮忙??

【问题讨论】:

    标签: c# asp.net sorting gridview


    【解决方案1】:

    你可以像这样添加图片。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      • 1970-01-01
      • 2015-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多