Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
want to use checkbox in gridview and 1 more thing that i m using gridview in which i m using checkbox and when it will show result then 1 checkbox will be checked bydefault because of database entry....i want to checked another 1 at run time and want to uncheckes the selected 1.how it can possible help me....
Posted

You can do this by writing some code as below in grid view RowDataBound event

C#
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
CheckBox chk = (CheckBox)e.Row.FindControl("<name of check box>");

if(DataBinder.Eval(e.Row.DataItem, "<database field>").ToString() == <somevalue>)
   chk.Checked= true;
else
   chk.Checked= false;
}



Good Luck
Keep Coding
 
Share this answer
 
v2
Use This Code On 'aspx' for Adding checkbox in gridview
ASP.NET
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
   <asp:CheckBox ID="chkSelect" runat="server" />
  </ItemTemplate>
</asp:TemplateField>


After this write code on aspx.cs
C#
foreach (GridViewRow row in gridview1.Rows)
        {
            for (int i = 0; i < gridview1.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)row.FindControl("chkSelect");
                chk.Checked = true;
            }
        }

try this..............
 
Share this answer
 
v2
If you want to uncheck previously checked checkbox after checking some other checkbox at runtime....this means you want to keep only 1 checkbox checked at a time.
If this is the case, i would suggest you to use radio button in place of checkbox.
 
Share this answer
 
Comments
pallavi ajin 4-Oct-11 7:56am    
ok i wl use radiobutton in gridview but i want one more thing that i want to save yes or no in database on the click of button.means i have 1 gridview in that i have radio button and at run time i want to save yes or no value in database after clicking on radiobutton.and after that submit button.means after selecting 1 radiobutton when user will click on the button it should be save in database with yes or no valiue....plz help me
nagendrathecoder 4-Oct-11 8:08am    
You need to loop through all rows of gridview and find radio button control on each row.
Then you can use Checked property of radio button on each row to save yes or no.
pallavi ajin 4-Oct-11 8:13am    
ya i m doing this but can u provide me coding for that i mean step by step.....plz
pallavi ajin 4-Oct-11 8:15am    
I M DOING THIS ON THE CLICK EVENT OF BUTTON
foreach (GridViewRow row in GridView1.Rows)
{
RadioButton rbtn = (RadioButton)row.FindControl("RadioButton1");
HiddenField hdfID = (HiddenField)row.FindControl("HiddenField1");
if (rbtn != null && rbtn.Checked)
{
strSQL = "update addrecipes SET [IsActive]=1 where ID=" + hdfID.Value + "";
du.ExecuteDML(strSQL);
}
}
nagendrathecoder 4-Oct-11 8:21am    
Are you getting any error in this code?
Why are you putting rbtn.Checked in if condition? This way u won't get false value.
gfbnvn

<code><a href=""></a>[<a href="" target="_blank"></a>]hjkjhkjhkjhkjhk
 
Share this answer
 

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