Accessing gridview row values during paging on another button click

Mark Fortune

New Member
\[code\]for (int i = 0; i < lstHoliday.Rows.Count; i++){ GridViewRow row = lstHoliday.Rows; bool isChecked = ((CheckBox)row.FindControl("Selector")).Checked; if (isChecked) { iHolidayId = Convert.ToInt32(row.Cells[0].Text); HolidayIds = iHolidayId + "," + HolidayIds; // Response.Redirect("Holidays.aspx"); }}\[/code\]This Code is inside a button click.During paging what happens is it shows only the number of rows of the current page\[code\]lstHoliday.Rows.Count = 8; // because the number of rows in gridview is greater \[/code\]I tried rebinding my gridview before this logic the correct number of rows are displayed but however the checkbox selection vanishes\[code\]lstHoliday.AllowPaging = false;lstHoliday.DataSource = dsHoliday;lstHoliday.DataBind();\[/code\]So the code never comes inside the \[code\]if\[/code\] blockI also tried \[code\]dsHoliday.Tables[0].Rows.Count\[/code\] but it throws an exception.\[code\]GridViewRow row = lstHoliday.Rows;\[/code\]As we cannot access more than 8 rows of the \[code\]Gridview\[/code\]Note: The states of the CheckBox persists through paging so that is not an issue.Using these methods to save state of checkboxes when using paging\[code\] //This method is used to populate the saved checkbox values private void PopulateCheckedValues() { ArrayList userdetails = (ArrayList)Session["CHECKED_ITEMS"]; if (userdetails != null && userdetails.Count > 0) { foreach (GridViewRow gvrow in lstHoliday.Rows) { int index = (int)lstHoliday.DataKeys[gvrow.RowIndex].Value; if (userdetails.Contains(index)) { CheckBox myCheckBox = (CheckBox)gvrow.FindControl("Selector"); myCheckBox.Checked = true; } } } } //This method is used to save the checkedstate of values private void SaveCheckedValues() { ArrayList userdetails = new ArrayList(); int index = 0; foreach (GridViewRow gvrow in lstHoliday.Rows) { index = (int)(lstHoliday.DataKeys[gvrow.RowIndex].Value); bool result = ((CheckBox)gvrow.FindControl("Selector")).Checked; // Check in the Session if (Session["CHECKED_ITEMS"] != null) userdetails = (ArrayList)Session["CHECKED_ITEMS"]; if (result) { if (!userdetails.Contains(index)) userdetails.Add(index); } else userdetails.Remove(index); } if (userdetails != null && userdetails.Count > 0) Session["CHECKED_ITEMS"] = userdetails; }\[/code\]
 
Back
Top