在使用sqldatasource、GridView控制項聯用時,如果報上述錯誤,就說明,GridView中的資料來源SqlDatasource主鍵欄位屬性沒有規定,應在GridView屬性設定中加入DataKeyNames=“id”。
Code
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid"
BorderWidth="1px" CellPadding="3" DataKeyNames="id" DataSourceID="SqlDataSource1" EmptyDataText="沒有可顯示的資料記錄。"
ForeColor="Black" GridLines="Vertical" Width="100%" PageSize="5">
<FooterStyle BackColor="#CCCCCC" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="zq" HeaderText="長貸期限" SortExpression="zq" />
<asp:BoundField DataField="cdje" HeaderText="長貸金額" SortExpression="cdje" />
</Columns>
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>