mercredi 24 décembre 2014

Gridview checkbox maintain state while paging gives error

I am having issue with my gridview



  1. It is giving me error while moving from one page to another

  2. It is not retaining checkbox state on paging


Getting error on KeepChecks


ERROR MESSAGE Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index ERROR MESSAGE


Below is the code...


Your help and assistance will be appriciated



public partial class AdminConsole : System.Web.UI.Page
{

SqlDataAdapter da;
DataSet ds = new DataSet();


SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
BindData();
}

}





private string GetConnectionString()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;

}

private void BindData()
{
SqlCommand cmd = new SqlCommand("SELECT UserID, EmployeeID, UserName, Dept FROM vw_UserID_Dept", con);

try
{
da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds);

con.Open();
cmd.ExecuteNonQuery();
con.Close();

//GVRegisterNewCarton.DataSource = ds;
//GVRegisterNewCarton.DataBind();

if (!object.Equals(ds.Tables[0], null))
{
if (ds.Tables[0].Rows.Count > 0)
{
GVRegisterNewCarton.DataSource = ds.Tables[0];
GVRegisterNewCarton.DataBind();
Session["MyTable"] = ds.Tables[0];
}
else
{
GVRegisterNewCarton.DataSource = null;
GVRegisterNewCarton.DataBind();
}
}
else
{
GVRegisterNewCarton.DataSource = null;
GVRegisterNewCarton.DataBind();
}
}
catch (Exception ex)
{

Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Error Binding Grid');", true);
lblStatus.Text = ex.Message;

}




}

protected void GVRegisterNewCarton_PageChanging(object sender, GridViewPageEventArgs e)
{
try
{
KeepChecks();
GVRegisterNewCarton.PageIndex = e.NewPageIndex;
BindData();
ApplyChecks();
}

catch (Exception ex)
{

Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Error Binding Grid');", true);
lblStatus.Text = ex.Message;

}

}




private void KeepChecks()
{
try
{
ArrayList chkList = new ArrayList();
int index = -1;
foreach (GridViewRow gvrow in GVRegisterNewCarton.Rows)
{
index = (int)GVRegisterNewCarton.DataKeys[gvrow.RowIndex].Value;
bool result = ((CheckBox)gvrow.FindControl("chkSelectAdd")).Checked;

if (Session["RemindChecks"] != null)
chkList = (ArrayList)Session["RemindChecks"];
if (result)
{
if (!chkList.Contains(index))
chkList.Add(index);
}
else
chkList.Remove(index);
}
if (chkList != null && chkList.Count > 0)
Session["RemindChecks"] = chkList;
}
catch (Exception ex)
{

Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Error Binding Grid');", true);
lblStatus.Text = ex.Message;

}
}


private void ApplyChecks()
{
try
{
ArrayList chkList = (ArrayList)Session["RemindChecks"];
if (chkList != null && chkList.Count > 0)
{
foreach (GridViewRow gvrow in GVRegisterNewCarton.Rows)
{
int index = (int)GVRegisterNewCarton.DataKeys[gvrow.RowIndex].Value;
if (chkList.Contains(index))
{
CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkSelectAdd");
myCheckBox.Checked = true;
}
}
}
}

catch (Exception ex)
{

Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Error Binding Grid');", true);
lblStatus.Text = ex.Message;

}
}




Aucun commentaire:

Enregistrer un commentaire