C# 从 ASP.NET GridView 单元格获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8829490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Value from ASP.NET GridView cell
提问by HGomez
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.
单击编辑按钮时,如何从 GrivView 单元格中获取值?我尝试过其他答案,但似乎都没有奏效。我希望能够在按下编辑按钮时获取该行的问卷 ID 的值。
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;
}
采纳答案by Brian Rogers
The GridViewEventArgshas the index of the row being edited. It doesn't look like you are using the index from the event args. Try this:
将GridViewEventArgs有被编辑的行的索引。看起来您没有使用事件参数中的索引。尝试这个:
protected void edit(object sender, GridViewEditEventArgs e)
{
string c = gvShowQuestionnaires.Rows[e.NewEditIndex].Cells[0].Text;
...
}
回答by Pupper
If you give your field an ID, you should be able to get it by calling
e.item.FindControl("fieldId").
如果你给你的字段一个 ID,你应该可以通过调用来获取它
e.item.FindControl("fieldId")。

