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 .

public partial class _Default : System.Web.UI.Page
{
    DataUtility dut = new DataUtility();
    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Con"]);

    protected void Page_Load(object sender, EventArgs e)
    {


        Label5.Text = (string)Session["Role_Name"];

        if (!IsPostBack)
        {

            populateGridview();

        }

    }
    public void populateGridview()
    {
        if (Session["Role_Name"] != null && !string.IsNullOrEmpty(Session["Role_Name"].ToString()))
        {
            string qry = "select ID,Pages,[Add],[View],Edit,[Delete] from Role1 where Role_Name= '" + Session["Role_Name"].ToString() + "'";

            DataSet ds = dut.GetDataSet(qry);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        //string qry = "select Pages,[Add],[View],Edit,[Delete] from Role1 where Role_Name= '"+Label5.Text+"'";
        //DataSet ds = dut.GetDataSet(qry);
        //GridView1.DataSource = ds;
        //GridView1.DataBind();
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    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("Add") as CheckBox);

                    CheckBox chkEdit = (row.Cells[3].FindControl("View") as CheckBox);

                    CheckBox chkView = (row.Cells[4].FindControl("Edit") as CheckBox);

                    CheckBox chkDelete = (row.Cells[5].FindControl("Delete") 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();

                    con.Close();

                }
            }
        }
        populateGridview();

        GridView1.DataBind();
    }

}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            if (((DataRowView)e.Row.DataItem)["Add"].ToString() == "Y")
            {
                CheckBox chktmp = (CheckBox)e.Row.FindControl("Add");
                chktmp.Checked = true;
            }
            if (((DataRowView)e.Row.DataItem)["View"].ToString() == "Y")
            {
                CheckBox chktmp = (CheckBox)e.Row.FindControl("View");
                chktmp.Checked = true;
            }
            if (((DataRowView)e.Row.DataItem)["Edit"].ToString() == "Y")
            {
                CheckBox chktmp = (CheckBox)e.Row.FindControl("Edit");
                chktmp.Checked = true;
            }
            if (((DataRowView)e.Row.DataItem)["Delete"].ToString() == "Y")
            {
                CheckBox chktmp = (CheckBox)e.Row.FindControl("Delete");
                chktmp.Checked = true;
            }

        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.