Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello All..
I have a Gridview with some fields and a Check box.I need to retrieve the selected check box values which are stored in database into the gridview.

Any Suggestions..?
Posted
Updated 24-Sep-12 19:07pm
v2

I assume that you need to check/uncheck gridview checkbox based on value present in the database. See below link, hope it helps you:
set-checkbox-in-gridview-based-on-datatable-value[^]
 
Share this answer
 
Do it on RowDataBound event of GridView.

C#
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
     CheckBox chk = (CheckBox)e.Row.FindControl("yourChekcBoxID");
     DataRowView drv = (DataRowView)e.Row.DataItem;

     chk.Checked = Convert.ToBoolean(drv["YourDataBaseField"]); 
  }
}
 
Share this answer
 
try below for 1 check box.

bool isChecked = (CheckBox)GridViewName.Rows.FindControl("chkBoxID")).Checked

Or

CheckBox ChkBox = (CheckBox)GridViewName.Rows.FindControl("chkBoxID")).value




try below methods for more than 1 check box

C#
// Select the checkboxes from the GridView control
for (int i = 0; i < GridView1.Rows.Count; i++)
{
  GridViewRow row = GridView1.Rows[i];
  bool isChecked = ((CheckBox) row.FindControl("chkSelect")).Checked;

  if (isChecked)
  {
   
  }
}


Thanks,
 
Share this answer
 
Comments
Afzal Shaikh 26-Sep-12 4:37am    
whats wrong in it?

please provide valid reason if anyone going to be devoted the solution of anyone.

Thanks,
firstly what r u storing in database when your check box is checked ,i mean tiny int or Boolean ,suppose u r storing as tiny int then u should try like this

Check on row data bound event
C#
protected void gvHardware_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
      // find check box 
      CheckBox ChkBox = (CheckBox)GridViewName.Rows.FindControl("chkBoxID"));
      // Now check the column of data table according to that u will check or uncheck the grid check box 
      strCheck = ((DataRowView)e.Row.DataItem)["operation"].ToString();
      if(strCheck=="1")
      {
         ChkBox .Checked=true;
      }
   }
}

hope this will help u
Thanks
 
Share this answer
 
v2

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