I have a GridView –
<asp:GridView ID="table_example" Runat="server"
DataSourceID="SqlDataSource3" AutoGenerateColumns="False"
ClientIDMode="Static" onprerender="table_example_PreRender">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="Label1" runat="server" Text="Select"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" OnCheckedChanged="CheckBoxPN_CheckedChanged" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="profileID" DataField="profileID"
SortExpression="profileID"></asp:BoundField>
<asp:BoundField HeaderText="profileName" DataField="profileName"
SortExpression="profileName"></asp:BoundField>
</Columns>
</asp:GridView>
Where the code behind for chkRow–
protected void CheckBoxPN_CheckedChanged(Object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridViewRow row = (GridViewRow)chk.NamingContainer;
//when a user clicks the checkbox I need the string from profileName in that row.
}
On debugging I can get to the checkBox changed event, however need to retrieve the string from the profileName cell from its respective row cell.
I tried something like this –
string c = row.Cells("profileName").value.toString();
However this did not work, how can I get the string?
If you use a
BoundFieldyou need to useCells[index].Text. Since it’s in the third column: