Deleting rows of datagrid and paging
Hai I have a problem in paging.
I have a datagrid with last column as checkbox. In the header of the lastcolumn i have a checkbox. If i choose the header checkbox, all the checkboxes in that particular page is checked and deleted. But once i click the delete buttpn the problem i encounter is
" Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount. "
Here is the complete code:
private void Page_Load(object sender, System.EventArgs e)
{
dgCustomerUpload.AllowPaging = true;
dgCustomerUpload.PagerStyle.Mode = agerMode.NumericPages;
dgCustomerUpload.PagerStyle.PageButtonCount = 10;
dgCustomerUpload.PageSize = 10;
if(!IsPostBack)
{
btnCustomerUpload.Attributes.Add("onClick", "javascript:return confirm('Are you sure you want to add these values?')");
BindData();
}
}
public void BindData()
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["CONN_STR"]);
if(conn.State != ConnectionState.Open){conn.Open();}
SqlDataAdapter adap = new SqlDataAdapter("SELECT * FROM [grp1].[dbo].v_gbims_CUSTOMER WHERE CUST_CD NOT IN (SELECT CUST_POWERID FROM [gbims].[dbo].t_CUSTOMER WHERE CUST_POWERID IS NOT NULL)",conn);
DataSet ds = new DataSet();
adap.Fill(ds,"t_CUSTOMER");
dgCustomerUpload.DataSource = ds.Tables["t_CUSTOMER"].DefaultView;
dgCustomerUpload.DataBind();
conn.Close();
}
bool ChecktheBox(int varInt)
{
if (varInt== 1)
{
return true;
}
else
{
return false;
}
}
private void btnCustomerUpload_Click(object sender, System.EventArgs e)
{
ArrayList selectedRows = new ArrayList();
foreach (DataGridItem item in dgCustomerUpload.Items)
{
CheckBox chkselection = (CheckBox)item.FindControl("chkselection");
if (chkselection != null && chkselection.Checked)
{
Label lblCUST_CD = (Label)item.FindControl("lblCUST_CD");
Label lblCUST_NAME = (Label)item.FindControl("lblCUST_NAME");
Label lblCUST_EMAIL_ADDR = (Label)item.FindControl("lblCUST_EMAIL_ADDR");
Label lblCUST_STATUS = (Label)item.FindControl("lblCUST_STATUS");
if (lblCUST_CD!= null)
{
string strCUST_CD = Regex.Replace(lblCUST_CD.Text,"'","''");
string strCUST_NAME =Regex.Replace(lblCUST_NAME.Text,"'","''");
string strCUST_EMAIL_ADDR =lblCUST_EMAIL_ADDR.Text;
string strCUST_STATUS =lblCUST_STATUS.Text;
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["CONN_STR"]);
if(conn.State != ConnectionState.Open){conn.Open();}
SqlCommand cmdCUST =new SqlCommand("INSERT INTO [gbims].[dbo].t_CUSTOMER (CUST_POWERID,CUST_NAME,CUST_EMAIL_ADDR,CUST_STATU S) VALUES (" + "'" + strCUST_CD + "'" + "," + "'" + strCUST_NAME + "'" + "," + "'" + strCUST_EMAIL_ADDR + "'" + "," + "'" + strCUST_STATUS + "'" + ")",conn);
cmdCUST.ExecuteNonQuery();
conn.Close();
chkselection.Checked=false;
}
}
}
BindData();
}
private void dgCustomerUpload_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEvent Args e)
{
dgCustomerUpload.CurrentPageIndex = e.NewPageIndex;
BindData();
}
|