【问题标题】:GridView Sorting TutorialGridView 排序教程
【发布时间】:2014-05-19 22:24:32
【问题描述】:

我想对网格视图的列进行排序。谁能给我解释一下过程。我需要什么方法以及如何去做。我在互联网上查找了一些,但它们似乎不起作用。我终于和 http://msdn.microsoft.com/en-us/librar/system.web.ui.webcontrols.gridview.sortexpression%28v=vs.110%29.aspx 当我点击箭头时它什么也没做。当我点击列名时,我得到: System.Web.HttpException: The GridView 'GridView1' 触发的事件排序未处理。

在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。

另外,如何将图像添加到所有列?

    protected void GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
    {

        // Use the RowType property to determine whether the 
        // row being created is the header row. 
        if (e.Row.RowType == DataControlRowType.Header)
        {
            // Call the GetSortColumnIndex helper method to determine
            // the index of the column being sorted.
            int sortColumnIndex = GetSortColumnIndex();

            if (sortColumnIndex != -1)
            {
                // Call the AddSortImage helper method to add
                // a sort direction image to the appropriate
                // column header. 
                AddSortImage(sortColumnIndex, e.Row);
            }
        }
    }

   private int GetSortColumnIndex(int p)
   {
       throw new NotImplementedException();
   }

    // This is a helper method used to determine the index of the
    // column being sorted. If no column is being sorted, -1 is returned.
   protected int GetSortColumnIndex()
    {

        // Iterate through the Columns collection to determine the index
        // of the column being sorted.
        foreach (DataControlField field in GridView1.Columns)
        {
            if (field.SortExpression == GridView1.SortExpression)
            {
                return GridView1.Columns.IndexOf(field);
            }
        }

        return -1;
    }


    // This is a helper method used to add a sort direction
    // image to the header of the column being sorted.
  protected  void AddSortImage(int columnIndex, GridViewRow headerRow)
    {

        // Create the sorting image based on the sort direction.
        Image sortImage = new Image();
        if (GridView1.SortDirection == SortDirection.Ascending)
        {
            sortImage.ImageUrl = "~/images/arrowasc.png";
            sortImage.AlternateText = "Ascending Order";
        }
        else
        {
            sortImage.ImageUrl = "~/images/arrowdesc.png";
            sortImage.AlternateText = "Descending Order";
        }

        // Add the image to the appropriate header cell.
        headerRow.Cells[2].Controls.Add(sortImage);
    }

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    这里有一个快速指南,让您可以使用带有排序功能的网格视图:

    您的 .aspx 页面(具有两列的 GridView):

    <asp:GridView ID="gridExistingQuestions" runat="server"
                  AutoGenerateColumns="False" GridLines="Vertical" 
                  EmptyDataText="No Question Found." AllowSorting="True"
                  OnSorting="gridExistingInterviewQuestions_Sorting" >
        <Columns>
           <asp:TemplateField HeaderText="Question Title" SortExpression="Title">
              <ItemTemplate>
                 <asp:Label ID="lblQuestionTitle" runat="server" 
                            Text='dynamic title here'></asp:Label>
              </ItemTemplate>
           </asp:TemplateField>
           <asp:TemplateField HeaderText="Question Desc" SortExpression="Desc">  
              <ItemTemplate>
                 <asp:Label ID="lblQuestionDesc" runat="server" 
                            Text='dynamic Desc here'></asp:Label>
              </ItemTemplate>
          </asp:TemplateField>
        </Columns>
    </asp:GridView>
    

    你的 aspx.cs 页面

     DataTable questionTable;
     DataView questionView;
    
     this.SetQuestionData();
        //on page gridview sorting
        protected void gridExistingQuestions_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                CurrentView = e.SortExpression + " " + this.ConvertSortDirectionToSql(e, "question");
                if (questionView != null)
                {
                    questionView.Sort = CurrentView;
    
                    // bind your grid view here ...
                }
                else
                    this.SetQuestionData();
            }
            catch { // display error }
        }
    
        private string CurrentView
        {
            get { return (string)(ViewState["vsCurrentView"] ?? (object)null); }
            set { ViewState["vsCurrentView"] = value; }
        }
    
        //handling sorting function
        private string ConvertSortDirectionToSql(GridViewSortEventArgs e, string key)
        {
            ViewState[key + e.SortExpression] = ViewState[key + e.SortExpression] ?? "ASC";
            ViewState[key + e.SortExpression] = (ViewState[key + e.SortExpression].ToString() == "ASC") ? "DESC" : "ASC";
            return ViewState[key + e.SortExpression].ToString();
        }
    
        private bool SetQuestionData()
        {
            questionTable = //get your data from db
    
            if (questionTable != null)
            {
                questionView = new DataView(questionTable);
    
                if (!string.IsNullOrEmpty(CurrentView))
                    QuestionView.Sort = CurrentView;
    
                // bind your grid view here
                return true;
            }
            return false;
        }
    

    您也可以对页面上的所有其他网格视图使用 ConvertSortDirectionToSql() 方法。

    希望它能给你一个线索,记住你需要在评论的地方绑定你的数据

    【讨论】:

      猜你喜欢
      • 2011-03-17
      • 2016-07-31
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多