Mana
Mana

Reputation: 1929

get checkbox state in in gridview

how can i get the checkbox state of my checkboxes inside a gridview? i have programmatically added them like so,

  foreach (GridViewRow gvr in GridView1.Rows)
  {
     tbCell = new TableCell();
     cbGV = new CheckBox();
     tbCell.Controls.Add(cbGV);
     gvr.Cells.Add(tbCell);
  {

Normally i would get the state of the checkboxes like so(below) when they are inside an itemtemplate, but this is not working, so any ideas guys ??

  foreach (GridViewRow getRowItems in GridView1.Rows)
  {
      chkBox = (CheckBox)(getRowItems.Cells[0].FindControl("cbGV"));

      if(chkBox.Checked == false) 
      {
      chkBox.Checked = true;
      }
  }

Upvotes: 1

Views: 1242

Answers (2)

Shiraj Momin
Shiraj Momin

Reputation: 685

foreach (GridViewRow gvr in GridView1.Rows)
{
 tbCell = new TableCell();
 cbGV = new CheckBox();
 cdGV.id="cbGV";
 tbCell.Controls.Add(cbGV);
 gvr.Cells.Add(tbCell);
}


foreach (GridViewRow getRowItems in GridView1.Rows)
{
  chkBox = (CheckBox)(getRowItems.Cells[0].FindControl("cbGV"));

  if(chkBox.Checked == false) 
  {
  chkBox.Checked = true;
  }

}

Upvotes: 0

Pranay Rana
Pranay Rana

Reputation: 176886

Try to add checkbox like this , i.e. as template column and than try to search this will also work rather than adding checkbox dynamically

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> 
<Columns> 
<asp:TemplateField> 
<ItemTemplate> 
<asp:CheckBox ID="cbGV" runat="server" /> 
</ItemTemplate> 
</asp:TemplateField> 
</Columns> 
</asp:GridView> 

this is not working because you forgot to give id to element

cbGV = new CheckBox();
cbGV.ID="cbGV";
tbCell.Controls.Add(cbGV);

try to give id as above and than try to find checkbox in row

Upvotes: 3

Related Questions