【问题标题】:Retrieving all GridViewRow objects from a GridView control with paging enabled从启用分页的 GridView 控件中检索所有 GridViewRow 对象
【发布时间】:2009-04-21 20:19:49
【问题描述】:

我目前在我的 aspx 页面上有一个启用分页的 GridView 控件,我需要遍历整个行集合/计数来处理选定的记录。使用我当前的代码,它只会循环遍历 GridView 行的当前页面。

完成这项任务的最佳方法是什么?

这是我当前的代码:

ASPX 页面:

<asp:GridView ID="MyGridView" runat="server" AllowPaging="true" PageSize="20">
   <Columns>
      <!-- My Column list -->
   </Columns>
</asp:GridView>
<asp:Button id="MyButton" runat="server" Text="Add" OnClick="MyButton_Click" />  

后面的代码:

protected void MyButton_Click(object sender, EventArgs e)
{
    for (int Count = 0; Count < MyGridView.Rows.Count; Count++)
    {
        //The row count is 20 and only contains the GridViewRow object in the current page view  
        //I want to retrieve the all GridViews rows so I can add them to a ReorderList control
    }   
}

【问题讨论】:

    标签: c# asp.net gridview paging


    【解决方案1】:

    是的,因为您的 gridview UI 只知道当前页面。 获取数据源并从那里确定行数...

            int count = ((DataTable)MyGridView.DataSource).Rows.Count;
    

    //或

            int count = ((ICollection<SomeRecord>)MyGridView.DataSource).Count;
    

    【讨论】:

    • 当我在按钮单击事件中使用此代码时,出现“对象引用未设置为对象的实例”异常。
    • 可能是因为当您引用它时,该对象不再可用。您可以在将数据源分配给 gridview 时尝试在 ViewState 中保存计数(使用上述方法),并在需要时再次检索它(在按钮单击中)。
    • @CRice 但是将数据存储在视图状态中比分页的目的要好,不是吗?
    • @Jules 在这里对这个问题进行了很多假设。不记得我对 ViewState 的想法是什么,但很可能需要在回发时再次请求数据源,然后可以重新评估项目数。
    【解决方案2】:

    只需使用以下代码:

    //Change gridview to
    GridView1.AllowPaging = false;
    GridView1.DataBind();
    
     //Transfer rows from GridView to table
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
        {
            for (int j = 0; j < GridView1.Rows[0].Cells.Count; j++)
            {
                  //Add your code here..
            }
        }
    }
    
    //After filling your datatable change gridview paging style back to first, ie.
    
    GridView1.AllowPaging = true;
    GridView1.DataBind();
    

    这可能对你有帮助,如果这对你有帮助,请告诉我...

    【讨论】:

    • 不,那是行不通的,因为 GridView1.Rows.Count 仅指单个页面的行,而不是其他页面中的整个 gridview 的行。
    【解决方案3】:

    我认为您应该从数据源的行数中获取行数。

    如果需要过滤行,可以使用DataTable的/DataView的Select方法。

    编辑:如果 gridview 被分页,您无法通过 gridview.Rows.Count 获得实际的行数。根据您的评论,我假设您使用 listDataSource 通用列表来绑定您的 gridview,您可以将您的行数设为:

    List<DataSourceItem> selectedRows = 
      listDataSource.FindAll(delegate(DataSourceItem item)
      {
          // Assuming you have a IsSelected bool property 
          // that refers your row is selected : 
          return item.IsSelected;
      });
      int rowCount = selectedRows.Count;
    

    【讨论】:

    • 我使用自定义类的通用列表作为 GridView 的数据源
    • 另外,我需要从这个 GridView 添加选定的记录,并将它们附加到 Ajax 控件工具包 ReorderList 控件。
    • 我更新了我的答案,您可以将 selectedRows 通用列表用于您的 ReorderList 控件。
    【解决方案4】:

    使用会话或状态来存储:

    protected void Set_CheckboxStatus()
        {
            CheckBox selectall = (CheckBox)EmployeeGrid.HeaderRow.FindControl("gcb_selectall");
            ArrayList cbstatuslist = new ArrayList();
            if (Session["childcbstatus"] != null)
            {
                cbstatuslist = (ArrayList)Session["childcbstatus"];
            }
            foreach (GridViewRow row in EmployeeGrid.Rows)
            {
                int cb_index = (int)row.DataItemIndex;  //For Getting DataItemIndex of EmployeeGrid 
                //int cb_index = (int)row.RowIndex;
                CheckBox cb_selemp = (CheckBox)row.FindControl("gcb_selemp");
                CheckBox cb_active = (CheckBox)row.FindControl("gcb_active");
    
                if (cb_selemp.Checked == true)
                {
                    if (!cbstatuslist.Contains(cb_index))
                        cbstatuslist.Add(cb_index);
                }
                else
                {
                    cbstatuslist.Remove(cb_index);
                }
            }
            Session["childcbstatus"] = cbstatuslist;
        }
    

    从数组列表中,您可以获取所有行索引以循环并通过分页从网格视图中获取值。

    【讨论】:

      【解决方案5】:

      @CRice 的回答应该是官方的回答。

      这是我的解决方案。您需要通过其DataSource 将gridview 的数据预先保存到ViewStateSession

      GridView.Rows 仅指“可见”行,或当前显示在屏幕上的页面。

          protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
          {
              GridView gv = (GridView)sender;
      
              DataSourceSelectArguments dss = new DataSourceSelectArguments();
      
          //get the datasource related to the gridview
          string wsDataSourceID = (gv.DataSourceID == string.Empty) ? ViewState["DataSourceID"].ToString() : gv.DataSourceID;
          SqlDataSource sds = (SqlDataSource)pnlMAIN.FindControl(wsDataSourceID);
          if (sds != null)
          {
              //load the data again but this time into a dataview object
              DataView dv = (DataView)sds.Select(DataSourceSelectArguments.Empty);
              if (dv != null)
              {
                  //convert the dataview to a datatable so we can see the row(s)
                  DataTable dt = (DataTable)dv.ToTable();
                  if (dt != null)
                  {
                      //Save your data before changing pages
                      ViewState["AllTheData"] = dt;
      
                      gv.DataSource = dt;
                      gv.DataSourceID = null;
                  }
              }
          }
      
          //now change pages!
              gv.PageIndex = e.NewPageIndex;
              gv.DataBind();
          }
      

      接下来,换页的时候,我们这里保存数据

      protected void GridView_PageIndexChanged(object sender, EventArgs e)
      {
          GridView gv = (GridView)sender;
      
          DataSourceSelectArguments dss = new DataSourceSelectArguments();
      
          //reload the datatable back to the gridview
          gv.DataSource = ViewState["AllTheData"];
          gv.DataSourceID = null;
          gv.DataBind();
      

      我希望代码不言自明。

      谢谢

      【讨论】:

        猜你喜欢
        • 2012-10-05
        • 1970-01-01
        • 2011-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-14
        • 1970-01-01
        • 2014-05-16
        相关资源
        最近更新 更多