从启用分页的 GridView 控件中检索所有 GridViewRow 对象

发布于 2024-07-17 09:40:08 字数 838 浏览 8 评论 0原文

目前,我的 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
    }   
}

I currently have a GridView control on my aspx page with paging enabled and I need to loop through the entire row collection/count to process the selected records. With my current code, it will only loop through the current page of GridView row.

What is the best way to accomplish this task?

Here is my current code:

ASPX page:

<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" />  

code behind:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烟─花易冷 2024-07-24 09:40:08

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

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

//或者

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

Yes because your gridview UI is only aware of the current page.
Get the datasource and determine the row count from there...

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

//or

        int count = ((ICollection<SomeRecord>)MyGridView.DataSource).Count;
我爱人 2024-07-24 09:40:08

只需使用以下代码:

//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();

这可能对您有帮助,请告诉我这是否对您有帮助......

Simply use the following code:

//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();

This may help you, let me know if this was helpful for you...

悲念泪 2024-07-24 09:40:08

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

如果需要过滤行,可以使用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;

I think you should get the row count from your data source's row count.

If you need to filter rows, you can use DataTable's / DataView's Select method.

EDIT : You can not get actual row count by gridview.Rows.Count if gridview is paged. Depending on your comment, I assume that you're using listDataSource generic list to bind your gridview, you can get your row count as :

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;
追星践月 2024-07-24 09:40:08

使用会话或状态来存储:

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

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

use session or state to store:

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

from the arraylist you can get all row index to loop and get the value from the gridview with paging.

终遇你 2024-07-24 09:40:08

@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();

希望代码能说明一切。

谢谢

@CRice's answer should have been the official answer.

Here is my solution. You need to presave the gridview's data, via its DataSource, into ViewState or Session.

GridView.Rows only refers to the "visible" rows, or the page currently shown on the screen.

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

Next, when changing pages, here we save the data

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

I hope the code speaks for itself.

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文