Reputation: 1534
How would I get the value from a GrivView cell when the edit button is clicked? I have tried other answer but none seem to work. I would like to be able to get the value of Questionnaire ID for the row when the edit button is pressed.
Here is the gridview im working with.
<asp:GridView runat="server" ID="gvShowQuestionnaires" HeaderStyle-CssClass="table_header" CssClass="view" AlternatingRowStyle-CssClass="alt" AutoGenerateColumns="False"
DataKeyNames='QuestionnaireID' OnRowDeleting="gvShowQuestionnaires_RowDeleting" OnRowEditing="edit" ShowFooter="true" FooterStyle-CssClass="view_table_footer">
<Columns>
<asp:BoundField DataField="QuestionnaireID" HeaderText="ID" HeaderStyle-Width="80px" ItemStyle-CssClass="bo"></asp:BoundField>
<asp:BoundField DataField="QuestionnaireName" HeaderText="Questionnaire Name" />
<asp:TemplateField HeaderText="Results" HeaderStyle-Width="150px"></asp:TemplateField>
<asp:CommandField HeaderText="Options" ShowDeleteButton="True" ShowEditButton="true" ItemStyle-CssClass="cart_delete">
</asp:CommandField>
</Columns>
</asp:GridView>
<asp:label ID="ab" runat="server"></asp:label>
The backend
protected void edit(object sender, GridViewEditEventArgs e)
{
string c = gvShowQuestionnaires.Rows[index].Cells[0].Text;
ab.Text = c;
}
Upvotes: 3
Views: 59983
Reputation: 129697
The GridViewEventArgs
has the index of the row being edited. It doesn't look like you are using the index from the event args. Try this:
protected void edit(object sender, GridViewEditEventArgs e)
{
string c = gvShowQuestionnaires.Rows[e.NewEditIndex].Cells[0].Text;
...
}
Upvotes: 8
Reputation: 2355
If you give your field an ID, you should be able to get it by calling
e.item.FindControl("fieldId")
.
Upvotes: 1