On my page there is a grid with pagination. It has fields to check that can not be lost when paging, but my grid is being loaded by Linq.
I tried to do it this way but it did not work:
protected void grdControleUnimed_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
foreach (GridViewRow row in grdControleUnimed.Rows)
{
var chkSelect = row.FindControl("chkSelect") as CheckBox;
IDataItemContainer container = (IDataItemContainer)chkSelect.NamingContainer;
if (chkSelect.Checked)
{
PersistRowIndex(container.DataItemIndex);
}
else
{
RemoveRowIndex(container.DataItemIndex);
}
}
ControleUnimedBLL bll = new ControleUnimedBLL();
grdControleUnimed.PageIndex = e.NewPageIndex;
List<CAD_TBL_CONTROLEUNIMED> list = new List<CAD_TBL_CONTROLEUNIMED>();
list = bll.GetWhere(Util.String2Short(txtEmpresa.Text), Util.String2Int32(txtMatricula.Text), txtSubMatricula.Text, txtNome.Text, txtCodIdentificacao.Text, ddlNomeUnimed.SelectedValue, ddlMovimentacao.SelectedValue, Util.String2Date(txtDatMovimentacaoIni.Text), Util.String2Date(txtDatMovimentacaoFim.Text), Util.String2Date(txtDatSaidaIni.Text), Util.String2Date(txtDatSaidaFim.Text)).ToList();
{
DataTable dt = list.ToDataTable();
//grdControleUnimed.DataSource = dt;
grdControleUnimed.DataBind();
RePopulateCheckBoxes();
}
}
private void PersistRowIndex(int index)
{
if (!SelectedUnimedIndex.Exists(i => i == index))
{
SelectedUnimedIndex.Add(index);
}
}
private void RemoveRowIndex(int index)
{
SelectedUnimedIndex.Remove(index);
}
private List<Int32> SelectedUnimedIndex
{
get
{
if (Session["SELECTED_UNIMED_INDEX"] == null)
{
Session["SELECTED_UNIMED_INDEX"] = new List<Int32>();
}
return (List<Int32>)Session["SELECTED_UNIMED_INDEX"];
}
}
private void RePopulateCheckBoxes()
{
foreach (GridViewRow row in grdControleUnimed.Rows)
{
var chkSelect = row.FindControl("chkSelect") as CheckBox;
IDataItemContainer container = (IDataItemContainer)chkSelect.NamingContainer;
if (SelectedUnimedIndex != null)
{
if (SelectedUnimedIndex.Exists(i => i == container.DataItemIndex))
{
chkSelect.Checked = true;
}
}
}
}