C# – Get Value from ASP.NET GridView cell

asp.netc++gridview

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;

        }

Best Solution

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;
    ...
}