2009-04-21 30 views
0

我目前在啓用分頁的aspx頁面上有一個GridView控件,我需要遍歷整個行集合/計數以處理選定的記錄。使用我當前的代碼,它只會遍歷GridView行的當前頁面。從GridView控件中檢索所有GridViewRow對象並啓用分頁

完成此任務的最佳方法是什麼?

這裏是我當前的代碼:

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

回答

1

我想你應該從你的數據源中的行數得到的行數。

如果需要過濾行,可以使用DataTable的/ DataView的選擇方法。

編輯:如果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; 
+0

我用我的自定義類的泛型列表作爲我的GridView的數據源 – 2009-04-21 20:26:20

+0

另外,我需要從這個GridView控件將其添加所選記錄,並追加到Ajax控件工具包ReorderList控制。 – 2009-04-21 20:32:01

+0

我更新了我的答案,您可以爲您的ReorderList控件使用selectedRows通用列表。 – Canavar 2009-04-21 20:46:03

3

是因爲你的GridView UI只知道當前頁面的。 獲取數據源,並確定從那裏行數...

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

//或

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

只需使用下面的代碼:

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

這可能會幫助你,讓我知道這是否對你有幫助...

0

使用會話或狀態來存儲:

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 arraylist你可以得到所有的行索引來循環,並從gridview中獲取值與分頁。

0

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

我希望的代碼是不言而喻的。

感謝