Click here to Skip to main content
16,004,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

i have four columns in a gridview .Every field is a template field with checkboxes.I have a button. when i check the check boxes and store it in database the checkbox value is storing. but after clicking the button the checkboxes unchecked. how to make the checkboxes checked .
Posted
Comments
syed shanu 14-Apr-14 4:47am    
rebind the grid after button click.i think when you bind the data to grid if the check box colum is true then the check box will be as checked.
Member 10578683 14-Apr-14 5:02am    
i did like this but checkboxes are not checked. my code is
protected void Button7_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
if (Session["Role_Name"] != null && !string.IsNullOrEmpty(Session["Role_Name"].ToString()))
{
CheckBox chkAdd = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
CheckBox chkEdit = (row.Cells[3].FindControl("CheckBox2") as CheckBox);
CheckBox chkView = (row.Cells[4].FindControl("CheckBox3") as CheckBox);
CheckBox chkDelete = (row.Cells[5].FindControl("CheckBox4") as CheckBox);
// string strID = row.Cells[0].Text;
string ID = row.Cells[0].Text;
string Pages = row.Cells[1].Text;
con.Open();
string query = "Update Role1 set [Add]='" + (chkAdd.Checked == true ? 'Y' : 'N') + "', [View]='" + (chkView.Checked == true ? 'Y' : 'N') + "' ,[Edit]='" + (chkEdit.Checked == true ? 'Y' : 'N') + "' ,[Delete]='" + (chkDelete.Checked == true ? 'Y' : 'N') + "' where Pages ='" + Pages + "' and Role_Name='" + Session["Role_Name"].ToString() + "'and ID='"+ID+"'";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
populateGridview();

con.Close();
GridView1.DataBind();
}
}
}
}

1 solution

protected void gvAppSelector_RowDataBound(object sender, GridViewRowEventArgs e)
{

DataRowView drv = (DataRowView)e.Row.DataItem;

if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (GridViewRow row in gvAppSelector.Rows) // For each row from the gridview
{
if ((row.FindControl("Yourcheckbox Id")as CheckBox).Checked) // Check for each row wheather checkbox has benn checked or not
}
}
}
 
Share this answer
 
Comments
Member 10578683 14-Apr-14 5:10am    
i have posted my code above. how will i do in that

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900