我的 GridView 的 DataKeyNames 问题

发布于 2024-12-05 06:29:34 字数 1973 浏览 3 评论 0原文

我的 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 技术交流群。

扫码二维码加入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" to DataKeyNames="GameID" ? If so, you also have to add the GameID into your select query from SqlDatasource. Your pasted code appears only take Team 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文