使用vb将数据绑定到asp.net中的gridview时出错

使用vb将数据绑定到asp.net中的gridview时出错,asp.net,asp.net-mvc,Asp.net,Asp.net Mvc,我遇到以下错误,无法解决 <%@ Page Language="VB" MasterPageFile ="~/Master.master" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <asp:GridV

我遇到以下错误,无法解决

<%@ Page Language="VB" MasterPageFile ="~/Master.master"  AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:GridView ID="GridView1"  PageSize ="50" runat="server" AllowPaging="True"    AllowSorting="True"
           AutoGenerateColumns="False" DataKeyNames="ID">
         <Columns>
            <asp:TemplateField HeaderText="ID">
                <ItemTemplate>
                    <%# Container.DataItemIndex + 1 %>
                 </ItemTemplate>
             </asp:TemplateField>
             <asp:BoundField DataField="Dname" HeaderText="Player" SortExpression="Dname" />
             <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
             <asp:BoundField DataField="GTScore" HeaderText="Score" 
                 SortExpression="GTScore" />
          </Columns>
        </asp:GridView>
</asp:Content>
这是我的.aspx.vb代码。我只是从函数选择器ToppersofMonth1调用存储过程

我在存储过程中的select查询是

SELECT TOP 100 A.[dispName] as [Dname], A.[city] as [City], SUM(B.[score]) as [GTScore]

FROM [Players] A,[Games] B 

WHERE A.[ID]=B.[playerID] AND B.[startedOn] BETWEEN @fromDate AND @toDate 

GROUP BY A.[dispName], A.[city] ORDER BY [GTScore] DESC
我犯了一个错误

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'ID'.
Description: An unhandled exception occurred during the execution of the current web       request. Please review the stack trace for more information about the error and     where it originated in the code.

Exception Details: System.Web.HttpException: DataBinding: 'System.Data.DataRowView'  does not contain a property with the name 'ID'.

您在DataKeyNames属性中引用ID,但它不在sql查询中。这正常吗?

只需从页面标记中删除它:
DataKeyNames=“ID”
,因为您的查询没有返回任何
ID
列,这会破坏绑定。或者修改您的
SQL
查询以同时返回ID列。

他没有将ID绑定到标题中;他正试图将ID设置为DataKeyName,以便网格知道绑定数据中哪个列是PK。我正在编辑我的答案,然后我看到了你的评论。。。你太快了非常感谢。。我是asp.net的新手,我甚至不知道DataKeyName的含义,我只是从其他gridview复制粘贴了它..:D
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'ID'.
Description: An unhandled exception occurred during the execution of the current web       request. Please review the stack trace for more information about the error and     where it originated in the code.

Exception Details: System.Web.HttpException: DataBinding: 'System.Data.DataRowView'  does not contain a property with the name 'ID'.