我的 GridView 的 DataKeyNames 问题
我的 DataKeyNames
值一直存在此问题。每行的主键是 GameID,但每当我将 DataKeyNames 设置为 GameID 时,它就不起作用。
我在网格视图中启用选择,因为我希望将附加信息放置在表单视图上。因此,当用户单击Select
时,表单视图会在该行上显示附加信息。
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" DataKeyNames="Team, Opponent"
ForeColor="#333333" GridLines="None" Height="360px" Width="641px"
onselectedindexchanged="GridView1_SelectedIndexChanged" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="Team" HeaderText="Team" SortExpression="Team" />
<asp:BoundField DataField="Opponent" HeaderText="Opponent"
SortExpression="Opponent" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [Team], [Opponent], [Date] FROM [game] WHERE ([Team] = @Team)">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" DefaultValue="Arsenal" Name="Team"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
I've been having this problem with my DataKeyNames
value. The primary key of each row is the GameID
but whenever I set the DataKeyNames
equal to the GameID
, it doesn't work.
I am enabling selection in my gridview because I want additional information to be placed on a formview. So when a user clicks Select
, the formview appears with additional information on the row.
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" DataKeyNames="Team, Opponent"
ForeColor="#333333" GridLines="None" Height="360px" Width="641px"
onselectedindexchanged="GridView1_SelectedIndexChanged" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="Team" HeaderText="Team" SortExpression="Team" />
<asp:BoundField DataField="Opponent" HeaderText="Opponent"
SortExpression="Opponent" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [Team], [Opponent], [Date] FROM [game] WHERE ([Team] = @Team)">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" DefaultValue="Arsenal" Name="Team"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的意思是改变
DataKeyNames="团队,对手"
改为DataKeyNames="GameID"
?如果是这样,您还必须将 GameID 添加到来自 SqlDatasource 的选择查询中。您粘贴的代码仅显示Team
和“Opponent”。更改
SelectCommand="SELECT [Team], [Opponent], [Date] FROM [game] WHERE ([Team] = @Team)">
至
SelectCommand="SELECT [GameID], [Team], [Opponent], [Date] FROM [game] WHERE ([Team] = @Team)">
假设该表中字段的名称,是GameID。
Do you mean change of
DataKeyNames="Team, Opponent"
toDataKeyNames="GameID"
? If so, you also have to add the GameID into your select query from SqlDatasource. Your pasted code appears only takeTeam
and ``Opponent.Change
SelectCommand="SELECT [Team], [Opponent], [Date] FROM [game] WHERE ([Team] = @Team)">
to
SelectCommand="SELECT [GameID], [Team], [Opponent], [Date] FROM [game] WHERE ([Team] = @Team)">
Assumes that the name of the field in the table, are GameID.