C# 如何在选定的索引更改属性上查找 gridview 的数据键值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17540461/
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 find datakey value of gridview on selected index changed property?
提问by Hitesh Kasar
My gridview is like this but I am getting error when I select view button to find primary key value column on selected index changed. Please help me to solve the issue.
我的 gridview 是这样的,但是当我选择视图按钮以查找所选索引上的主键值列已更改时出现错误。请帮我解决这个问题。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns >
<asp:TemplateField >
<ItemTemplate >
<asp:Button ID="btnViewComments" Text ="View Comments" runat ="server" CommandName ="select" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField ="forumId" Visible ="false" />
<%--<asp:CommandField ButtonType ="Button" ShowSelectButton ="true" SelectText ="View Comments"/>--%>
<asp:TemplateField HeaderText ="Question">
<ItemTemplate >
<asp:TextBox ID ="txtQuestion" Text ='<%#Eval("question")%>' runat ="server" TextMode ="MultiLine" Height="100" Width ="350"></asp:TextBox>
<%-- <%#Eval("question")%>--%>
</ItemTemplate>
<%--<EditItemTemplate >
<asp:TextBox ID ="txtQuestion" Text ='<%#Eval("question")%>' runat ="server" TextMode ="MultiLine" ></asp:TextBox>
</EditItemTemplate>--%>
</asp:TemplateField>
<asp:TemplateField HeaderText="Poster Name">
<ItemTemplate >
<%#Eval("posterName") %>
</ItemTemplate>
<EditItemTemplate >
<asp:Label ID ="lblPosterName" Text ='<%#Eval("posterName") %>' runat ="server" ></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate >
<%#Eval("dateTim") %>
</ItemTemplate>
<EditItemTemplate >
<asp:Label ID ="lblDateTime" Text ='<%#Eval("dateTim") %>' runat ="server" ></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
my code is.....
我的代码是.....
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
Int64 forumId = (Int64)GridView1.SelectedValue;
Session["forumId"] = forumId;
Response.Redirect("Thread.aspx");
}
catch (Exception)
{
throw;
}
}
采纳答案by Maruti
First you have to define field name in grid view declaration that which field you want to make datakey. for example if you want "forumId" datakey.than
首先,您必须在网格视图声明中定义字段名称,即要制作数据键的字段。例如,如果你想要“forumId”datakey.than
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
DataKeyNames="forumId">
and than you can access in this way
并且您可以通过这种方式访问
int intforumid = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0]);
回答by Damith
you can set DataKeyNamesas forumIdlike below
你可以设置DataKeyNames为forumId像下面
<asp:GridView ID="GridView1" runat="server" DataKeyNames = "forumId" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
Since you haven't give any data key names in current solution GridView1.SelectedValuewill not contain the value you expected
由于您尚未在当前解决方案中提供任何数据键名称,因此GridView1.SelectedValue将不包含您期望的值
回答by Soner G?nül
Looks like you just need to set DataKeyNamesproperty to forumIdlike;
看起来您只需要设置DataKeyNames属性即可forumId;
<asp:GridView DataKeyNames = "forumId" ...
回答by Raghubar
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
Int64 forumId = Convert.ToInt64(GridView1.SelectedRow.Cells[1].Text);
Session["forumId"] = forumId;
Response.Redirect("Thread.aspx");
}
catch (Exception)
{
throw;
}
}
回答by AlphaRomeo69
You would need to specify the unique column name in the gridview to be set under the Datakey tab.
您需要在要在 Datakey 选项卡下设置的 gridview 中指定唯一的列名称。
From there, you would need to invoke the _selectedIndexChanged method on the behind page code.
从那里,您需要在后台代码中调用 _selectedIndexChanged 方法。
回答by Hema Shetty
If you are not using gridview select event in your page.cs code then you just remove OnSelectedIndexChanged="GridView1_SelectedIndexChanged" from the aspx code of page of gridview.
如果您没有在 page.cs 代码中使用 gridview select 事件,那么您只需从 gridview 页面的 aspx 代码中删除 OnSelectedIndexChanged="GridView1_SelectedIndexChanged" 。

