C# 如何从代码隐藏中的 GridView 访问选定的 Boundfield 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19857894/
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
How to access a selected Boundfield value from a GridView in code-behind
提问by Poorya
I have seen similar questions but none of the answers helped me to workout this problem. I have GridView with a ReadOnly field as follow.
我见过类似的问题,但没有一个答案能帮助我解决这个问题。我有一个带有只读字段的 GridView,如下所示。
GridView:
网格视图:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="projectID"
DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display."
PageSize="5" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"/>
<asp:BoundField DataField="prID" HeaderText="prID" SortExpression="prID"/>
<asp:BoundField DataField="projectName" HeaderText="projectName"
SortExpression="projectName" />
<asp:BoundField DataField="projectType" HeaderText="projectType"
SortExpression="projectType" />
</Columns>
<EditRowStyle CssClass="GridViewEditRow"/>
</asp:GridView>
as you can see the prID
BoundField has Readonly=True
attribute.
I'm trying to get the value of the prID
in code-behind when user is updating the other fields in the row.
如您所见,prID
BoundField 具有Readonly=True
属性。prID
当用户更新行中的其他字段时,我试图获取in 代码隐藏的值。
code-behind:
代码隐藏:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
String d1 = ((TextBox)(row.Cells[2].Controls[0])).Text;
String d2 = ((TextBox)(row.Cells[3].Controls[0])).Text;
// this only works while the field is not readonly
string prIDUpdate = ((TextBox)(row.Cells[1].Controls[0])).Text;
}
Note:I have tried using GridView1.DataKeys[e.RowIndex]
and also onRowDataBound
an d setting the BoundField ready only in code-behind but I haven't been able to get results
注意:我已经尝试使用GridView1.DataKeys[e.RowIndex]
并且onRowDataBound
仅在代码隐藏中设置 BoundField 准备就绪,但我无法获得结果
Thanks in advance!
提前致谢!
采纳答案by Alice
I saw that your DataKeyNames setting in GridView controlis like this
我看到你在GridView控件中的DataKeyNames设置是这样的
DataKeyNames="projectID"
Then I guess that your key name is projectIDnot prID, isn't it? If so, you could get data for the selected row as this line:
那么我猜你的键名是projectID而不是prID,不是吗?如果是这样,您可以获得所选行的数据作为这一行:
string id = GridView1.DataKeys[e.RowIndex]["projectID"].ToString();
And you should also add this column:
您还应该添加此列:
<asp:BoundField DataField="projectID" HeaderText="prID" SortExpression="projectID"/>
Did you try that?
你试过吗?
In other way, you could try to use TemplateField instead
换句话说,您可以尝试使用 TemplateField 代替
<Columns>
<asp:TemplateField HeaderText="prID" SortExpression="prID">
<ItemTemplate>
<asp:Label ID="lblPrId" runat="server" Text='<%# Bind("prID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="projectName" HeaderText="projectName"
SortExpression="projectName" />
<asp:BoundField DataField="projectType" HeaderText="projectType"
SortExpression="projectType" />
</Columns>
And this code to get data from prID column in GridView1_RowUpdating event handler:
此代码用于从 GridView1_RowUpdating 事件处理程序中的 prID 列获取数据:
Label lblPrId = row.FindControl("lblPrId") as Label;
string prId = lblPrId .Text;
Sorry if this doesn't help.
对不起,如果这没有帮助。