【问题标题】:DataKeyNames issue with my GridView我的 GridView 的 DataKeyNames 问题
【发布时间】:2011-11-19 09:52:07
【问题描述】:

我的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>

【问题讨论】:

  • SQL 数据源在查询中没有 GameID...这个值从何而来?

标签: asp.net sql gridview datakey


【解决方案1】:

您的意思是将
DataKeyNames="Team, Opponent" 更改为DataKeyNames="GameID" 吗?如果是这样,您还必须将 GameID 添加到来自 SqlDatasource 的选择查询中。您粘贴的代码似乎只需要 Team 和 ``Opponent.

更改
SelectCommand="SELECT [Team], [Opponent], [Date] FROM [game] WHERE ([Team] = @Team)"&gt;

SelectCommand="SELECT [GameID], [Team], [Opponent], [Date] FROM [game] WHERE ([Team] = @Team)"&gt;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多