Click here to Skip to main content
15,891,907 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a number of checkboxes in gridview. when i checked that its value is inserting into database. but when i am again debugging it is showing unchecked. i want to show it checked what are checked
Posted
Comments
Rockstar_ 10-Mar-14 4:36am    
Please elaborate the problem clearly.
Krunal Rohit 10-Mar-14 4:39am    
Make the question clear.

-KR
Member 10578683 10-Mar-14 4:49am    
i want to show the check boxes remains checked wnen i checked them in every time when i will open my website

Write your code in condition like
C#
if(!IsPostBack)
{
    // your chechbox logic
}

-KR
 
Share this answer
 
if you have already saved checked check boxes details in data base. Then go for row data bound event of grid view & by checking check property for each row ,check tat row check box .

for better understanding refer this link
 
Share this answer
 
Comments
Member 10578683 10-Mar-14 5:19am    
protected void Button3_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[2].FindControl("CheckBox1");
if (chk != null)
{
if (chk.Checked)
{
string strID = GridView1.Rows[i].Cells[0].Text;
string strName = GridView1.Rows[i].Cells[1].Text;
string status = "Y";
SqlCommand cmd = new SqlCommand();
cmd = new SqlCommand("Update Role1 set Checkin_Add ='" + status + "' where ID= '" + strID + "' and Role_Name= '" + strName + "'", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
else
{
string strID = GridView1.Rows[i].Cells[0].Text;
string strName = GridView1.Rows[i].Cells[1].Text;
string status = "N";
SqlCommand cmd = new SqlCommand();
//cmd = new SqlCommand("insert into Role1(Checkin_Add) values(" + status + " where ID= '" + strID + "' and Role_Name='" + strName + "')", con);
cmd = new SqlCommand("Update Role1 set Checkin_Add ='" + status + "' where ID= '" + strID + "' and Role_Name= '" + strName + "'", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}


}
Member 10578683 10-Mar-14 5:19am    
hi this my code what will i do
V5709 10-Mar-14 6:56am    
ok... This code is working for insert or update check box related details in data base.
Now you have to done one thing more. After inserting or updating in data base you should rebind your grid.(i think you may have done it on page load also.) here call that binding function again. If you have written row data bound event as explained in above link, then grid binding event will rise an call to row data bound event & there you will have placed your check box checking logic.
Member 10578683 10-Mar-14 9:50am    
when i m going from one page to other checkboxes are not in checked format
V5709 11-Mar-14 0:45am    
to show that you have to repopulate data from data base & have to show it in grid.

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