我的GridView出现DataKeyNames问题

时间:2011-09-17 19:25:44

标签: asp.net sql gridview datakey

我的DataKeyNames值一直存在这个问题。每行的主键是GameID,但每当我将DataKeyNames设置为GameID时,它都不起作用。

我在gridview中启用选择,因为我希望将更多信息放在formview上。因此,当用户点击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>

1 个答案:

答案 0 :(得分:0)

你的意思是改变 DataKeyNames="Team, Opponent"DataKeyNames="GameID"?如果是这样,您还必须将GameID从SqlDatasource添加到您的选择查询中。粘贴的代码只显示Team和“对手”。

更改
SelectCommand="SELECT [Team], [Opponent], [Date] FROM [game] WHERE ([Team] = @Team)">
SelectCommand="SELECT [GameID], [Team], [Opponent], [Date] FROM [game] WHERE ([Team] = @Team)">

假设表中字段的名称是GameID。