在运行时更改gridview排序图像

时间:2011-07-30 12:44:18

标签: .net asp.net visual-studio sorting gridview

我希望在特定列的排序完成后在运行时更改gridview排序图像,以便用户可以识别排序的哪个列和方向。

说,有10列,客户ID,名称等。我对客户名称进行了降序排序,然后降序排序图像应该更改为其他图像,以便于识别。

这怎么可能?

1 个答案:

答案 0 :(得分:0)

以下是我将如何做,这是一个有效的解决方案,它的工作原理:

 OnRowCreated="gvInActive_RowCreated" 

protected void gvInActive_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            if (!(e.Row == null) && e.Row.RowType == DataControlRowType.Header)
            {
                #region sorting
                foreach (TableCell cell in e.Row.Cells)
                {
                    if (cell.HasControls())
                    {
                        if (cell.Controls[0].ToString() != "System.Web.UI.LiteralControl")
                        {
                            LinkButton button = (LinkButton)cell.Controls[0];
                            if (!(button == null))
                            {
                                System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();
                                //image.ImageUrl = "images/nosort.gif";
                                image.Visible = false;
                                if (this.gvInactive.SortExpression == button.CommandArgument)
                                {
                                    if (gvInactive.SortDirection == System.Web.UI.WebControls.SortDirection.Ascending)
                                    {
                                        image.Visible = true;
                                        image.ImageUrl = "../../images/sort_asc.gif";
                                    }
                                    else
                                    {
                                        image.Visible = true;
                                        image.ImageUrl = "../../images/sort_desc.gif";
                                    }
                                }
                                //else
                                //{
                                //    image.Visible = true;
                                //    image.ImageUrl = "images/sort_none.gif";
                                //}
                                cell.Controls.Add(image);
                            }
                        }
                    }
                }
                #endregion
            }
        } 
相关问题