1

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>
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
omar
  • 11
  • 2

1 Answers1

0

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.

Independent
  • 2,924
  • 7
  • 29
  • 45