使用分页计算gridview的总行数

我有一个带分页的GridView。 当我尝试使用gridview.rows.count计算gridview行时,它仅为当前页面提供行计数。

无论页面索引如何,我如何获得GridView的总行数?

//这是.aspx页面的代码

   
 
Search Candidates
<%----%>
Select Company Select Department -Select Department-
Select Vacancy -Select Vacancy-
 
App.ID <asp:Label ID="appIdLbl" runat="server" Text=''> First Name <asp:Label ID="firstNameLbl" runat="server" Text=''> Last Name <asp:Label ID="lastNameLbl" runat="server" Text=''> Qualification <asp:Label ID="qualiNameLbl" runat="server" Text=''>  Experience <asp:Label ID="Label1" runat="server" Text=''> EmailId <asp:Label ID="emailLbl" runat="server" Text=''> Send Mail
 
 
 
 
function alertOnBadSelection() { var select = document.getElementById('companyList'); if (select.options[select.selectedIndex].value == "-Select Company-") { alert('Please Select Company!'); return false; } }

//这是我的aspx.cs页面的代码

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Collections; public partial class HR_Department_searcAppForVac : System.Web.UI.Page { DataOperation oDo = new DataOperation(); AppVacancyDetailClass objAppVacDetail = new AppVacancyDetailClass(); protected void Page_Load(object sender, EventArgs e) { notifyLbl.Visible = false; titleLbl.Visible = false; sendMailBtn.Visible = false; try { if (!IsPostBack) { // Disable department dropdown list and vacancy dropdown list till company is not selected. deptList.Enabled = false; vacanyList.Enabled = false; //Fill Company dropdown list. DataTable objCmpnyTable = oDo.DropDownList("select * from tblCompanyMaster"); if (objCmpnyTable.Rows.Count > 0) { companyList.DataSource = objCmpnyTable; companyList.DataValueField = "CompId"; companyList.DataTextField = "CompName"; companyList.DataBind(); } else { notifyLbl.Visible = true; notifyLbl.Text = "There is not any company in the list."; } } else { if (companyList.SelectedIndex  0) { deptList.Enabled = true; deptList.Items.Clear(); string str = "select * from vwCompWiseList where CompId=" + companyList.SelectedValue; DataTable objDeptTable = oDo.DropDownList("select DeptId,DeptName from vwCompWiseDept where CompId= "+companyList.SelectedValue); if (objDeptTable.Rows.Count > 0) { deptList.DataSource = objDeptTable; deptList.DataTextField = "DeptName"; deptList.DataValueField = "deptId"; deptList.DataBind(); deptList.Items.Insert(0, new ListItem("--Select Department--", "-1")); } else { deptList.Items.Insert(0, new ListItem("--No Departments--", "-1")); notifyLbl.Visible = true; notifyLbl.Text = "No Departments Available in " + companyList.SelectedItem.Text; } } else { notifyLbl.Visible = true; notifyLbl.Text = "Select Company...."; appForVacGrid.DataSource = null; appForVacGrid.DataBind(); } } catch (Exception) { throw; } } protected void deptList_SelectedIndexChanged(object sender, EventArgs e) { try { if (deptList.SelectedIndex > 0) { vacanyList.Enabled = true; vacanyList.Items.Clear(); DataTable objVacancytbl = oDo.DropDownList("select VacId,VacTitle from tblVacancyMaster where DeptId =" + deptList.SelectedValue + " and CompId=" + companyList.SelectedValue); if (objVacancytbl.Rows.Count > 0) { vacanyList.DataSource = objVacancytbl; vacanyList.DataValueField = "VacId"; vacanyList.DataTextField = "VacTitle"; vacanyList.DataBind(); vacanyList.Items.Insert(0, new ListItem("--Select Vacancy--", "-1")); appForVacGrid.DataSource = null; appForVacGrid.DataBind(); } else { notifyLbl.Visible = true; notifyLbl.Text = "ALL VACANCIES ARE CLOSED IN "+" "+deptList.SelectedItem.Text.ToUpper(); vacanyList.Enabled = false; appForVacGrid.DataSource = null; appForVacGrid.DataBind(); } } else { notifyLbl.Visible = true; notifyLbl.Text = "Select Department..."; appForVacGrid.DataSource = null; appForVacGrid.DataBind(); vacanyList.Enabled = false; } } catch (Exception) { throw; } } protected void vacanyList_SelectedIndexChanged(object sender, EventArgs e) { try { DataTable AppListTbl = objAppVacDetail.GetValue("CompId=" + companyList.SelectedValue + " and DeptId=" + deptList.SelectedValue + " and VacId=" + vacanyList.SelectedValue); if (AppListTbl.Rows.Count > 0) { appForVacGrid.DataSource = AppListTbl; appForVacGrid.DataBind(); appForVacGrid.Columns[5].Visible = false; Session.Add("snAppListTbl", AppListTbl); titleLbl.Visible = true; titleLbl.Text = AppListTbl.Rows.Count.ToString() + " " + "CANDIDATE(S) ARE ELIGIBLE FOR THE POST OF" + " " + vacanyList.SelectedItem.Text.ToUpper() + "."; sendMailBtn.Visible = true; } else { notifyLbl.Visible = true; notifyLbl.Text = "ALL VACANCIES ARE CLOSED IN " + " " + deptList.SelectedItem.Text.ToUpper(); appForVacGrid.DataSource = null; appForVacGrid.DataBind(); } } catch (Exception) { throw; } } protected void appForVacGrid_PageIndexChanging(object sender, GridViewPageEventArgs e) { //DataTable AppListTbl = (DataTable)Session["snAppListTbl"]; titleLbl.Visible = true; titleLbl.Text = ((DataTable)Session["snAppListTbl"]).Rows.Count.ToString() + " " + "CANDIDATE(S) ARE ELIGIBLE FOR THE POST OF" + " " + vacanyList.SelectedItem.Text.ToUpper() + "."; appForVacGrid.PageIndex = e.NewPageIndex; appForVacGrid.DataSource = (DataTable)Session["snAppListTbl"]; appForVacGrid.DataBind(); sendMailBtn.Visible = true; } protected void sendMailBtn_Click(object sender, EventArgs e) { DataTable AppListTable = ((DataTable)Session["snAppListTbl"]); int intTotalRows = AppListTable.Rows.Count; string strFromId="",strToId="",strCcId="",strBccId=""; string strVacTitle="",strCompName="",strMailBody=""; string strSubject = "Rgarding Selection of Your Resume"; Label EmailLbl; //for (int intRow = 0; intRow < intTotalRows; intRow++) foreach (GridViewRow Row in appForVacGrid.Rows) { CheckBox objSendMail = (CheckBox)Row.FindControl("sendMailBox"); //CheckBox objSendMail = (CheckBox)appForVacGrid.Rows[Row].FindControl("sendMailBox"); if (objSendMail.Checked) { if (strToId == "") { //strToId = AppListTable.Rows[Row]["AppEmailId1"].ToString(); EmailLbl = (Label)Row.FindControl("emailLbl"); strToId = EmailLbl.Text; } else { //strToId += "," + AppListTable.Rows[Row]["AppEmailId1"].ToString(); EmailLbl = (Label)Row.FindControl("emailLbl"); strToId +=","+ EmailLbl.Text; } } } strVacTitle = AppListTable.Rows[intTotalRows]["VacTitle"].ToString(); strCompName = AppListTable.Rows[intTotalRows]["CompName"].ToString(); strMailBody = CommonProcedures.GetMailBody(strVacTitle, strCompName); //CommonProcedures.SendMail(strFromId, strToId, strCcId, strBccId, strSubject, null, strMailBody, false); } } 

现在我想发送邮件给所有选中复选框的候选人..我做了很多事情,但没有得到任何解决方案

是的,这是真的,它只会返回当前的页面行。 如果您真的想获得总行数,则需要从数据源获取。

就像…如果你有DataTable那么就像……

 DataTable.Rows.Count 

如果您正在使用sqldatasource或objectdatasource您需要在处理数据源的Selected事件时使用ObjectDataSourceStatusEventArgs或SqlDataSourceStatusEventArgs的ReturnValue。

如果使用sqldatasource,则可以使用“Selected”事件计算总行数,该事件在select操作完成后触发。

 protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e) { int rowCount = e.AffectedRows; } 

如果您正在使用对象数据源,请务必注意ODS上的Selected事件在返回数据集后会被调用两次,并再次调用您在SelectCountMethod属性中指定的方法。 只需测试以查看返回是否为事件中的Int32。

 protected void ObjectDataSource1_Selected(object sender, ObjectDataSourceStatusEventArgs e) { if (e.ReturnValue.GetType() == typeof(System.Int32)) int rowCount = (Int32)e.ReturnValue; } 

您可以在以下url找到一个工作示例: http : //www.webswapp.com/codesamples/aspnet20/dropdownlist_gridview/default.aspx

如果你的GridView被DataSet或DataTable填充:

 int rowCount=((DataTable)Customer).Rows.Count; 

如果要绑定列表或对象数组,则可以执行以下操作:

 int rowCount = ((Customer[])gv.DataSource).Count; 

是的,我认为使用PagedDataSource是一个更好的选择。 我正在使用它。

 PagedDataSource pds = new PagedDataSource(); pds.DataSource = dt_main.DefaultView; pds.AllowPaging = true; pds.PageSize = 8; int currentPage; if (Request.QueryString["page"] != null) { currentPage = Int32.Parse(Request.QueryString["page"]); } else { currentPage = 1; } pds.CurrentPageIndex = currentPage - 1; Label1.Text = "Page " + currentPage + " of " + pds.PageCount; if (!pds.IsFirstPage) { linkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage - 1); } if (!pds.IsLastPage) { linkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage + 1); } gridMain.DataSource = pds; gridMain.DataBind(); 

您可以使用分页数据源来绑定gridview,这将非常容易进行自定义分页并获取页数。

  if (Grid.DataSource == null) return 0; else if (Grid.DataSource.GetType() == typeof(DataTable)) return (Grid.DataSource as DataTable).Rows.Count; else if (Grid.DataSource.GetType().IsGenericType) return (Grid.DataSource as IList).Count; 

试试下面的代码

 int _TotalRecs = ((System.Data.DataView)GridView.DataSource).Count; int _CurrentRecStart = GridView.PageIndex * GridView.PageSize + 1; int _CurrentRecEnd = GridView.PageIndex * GridView.PageSize + GridView.Rows.Count; lblTitle.Text = string.Format("Displaying {0} to {1} of {2} records found", _CurrentRecStart, _CurrentRecEnd, _TotalRecs); 

例如输出将如下….

显示1到15,共找到67条记录。

try(gridView.DataSource as IEnumerable )。主集合的Count()

像这样

 (gridview1.DataSource as DataTable).Rows.Count(); 

如果使用数据表或其他数据源进行绑定,则可以显示数据源中的总记录。 例如

  if (dtLog != null && dtLog .Rows.Count >= 0) { lblTotal.Text = "Total " + Convert.ToString(dtLog .Rows.Count) + " records."; } 
 dim dt as datatable
dt=gridview1.datasource
msgbox (dt.rows.count)

如果您有一个将使用datasetdataset填充的gridview ,则计算dataset ds.table(0).rows.countdatasetds.table(0).rows.count )例如

 For cnt1 = 0 To Total_batchRecords - 1 dgchgsdtl1.Rows.Item(cnt1).FindControl("ControlName"), Label) Next 
  int a = grdvw.PageIndex; int rowcount=0; for (int i = 0; i < grdvw.PageCount; i++) { grdvw.SetPageIndex(i); foreach (GridViewRow row in grdvw.Rows) { if (row.RowType == DataControlRowType.DataRow) { rowcount++; } } } grdvw.SetPageIndex(a);