Click here to Skip to main content
15,920,031 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using a gridview in asp.net with a template field of type checkbox. I wanna retrieve the value of checkbox at runtime and using the following code..but its not working for me please help

C#
foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.Cells[1].Text.ToString() == "True" || row.Cells[1].Text.ToString() == "1")
                {
                    rtag = rtag + "1";

                }
                else
                {
                    rtag = rtag + "0";
                }
            }
Posted

Try this code.

C#
foreach (GridViewRow row in GridView1.Rows)
            {
bool isChecked = ((CheckBox) row.FindControl("YourCheckBoxID")).Checked;

                if (isChecked)
                {
                    rtag = rtag +"1"

                }
                else
                {
                    rtag = rtag + "0"
                }
            }
 
Share this answer
 
Comments
djrocks0101 9-Jul-12 9:29am    
what if I wanna set the checkbox's value?
Hi try the below sample.

C#
foreach (GridViewRow row in GridView1.Rows)
{
    CheckBox chk = (CheckBox)row.FindControl("YourCheckBoxID");
                if (chk.Checked)
                {
                    //logic to implement when check box is checked
                }
                else
                {
                    //logic to implement when check box is not checked
                }
}
 
Share this answer
 
v2
Comments
djrocks0101 9-Jul-12 9:30am    
If I wanna set the checked property of the check box in grid view then how it can be done?
please help..
__TR__ 9-Jul-12 13:34pm    
Try

((CheckBox)GridView1.Rows[2].FindControl("YourCheckBoxID"))).Checked = true;
OR
((CheckBox)row.FindControl("YourCheckBoxID")).Checked = true;

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