Reputation: 225
My code
<asp:GridView ID="FireFighters" runat="server" AutoGenerateColumns="false" OnRowDataBound="FireFighters_RowDataBound" >
<Columns>
<asp:BoundField HeaderText="שם הכבאי" DataField="username" />
<asp:CheckBoxField HeaderText="מצוות לאירוע" DataField="IsLinked" />
</Columns>
</asp:GridView>
I can't add and id to the asp:CheckBoxField, i tried calling it like this from code-behind:
foreach(GridViewRow gvr in FireFighters.Rows)
{
lst.Add(new FireFighter{username=gvr.Attributes["id"].ToString(),IsLinked=((gvr.Cells[1] as Control) as CheckBox).Checked});
}
But its null, how can i get the value(checked or not) of the Checkbox?
Upvotes: 1
Views: 435
Reputation: 17380
You are almost there. This should help you:
foreach (GridViewRow gvr in FireFighters.Rows)
{
lst.Add(new MyClass { username = ((DataControlFieldCell)gvr.Controls[0]).Text, IsLinked = ((CheckBox)gvr.Controls[1].Controls[0]).Checked });
}
Each row has a collection of cells, and these cells have the controls you want inside. You can access them by index (in the case of the checkbox) and cast it to a CheckBox
or access the cells themselves and get the text and cast it to DataControlFieldCell
.
Upvotes: 1