asp.net – DataBinding:’System.Data.DataRowView’不包含名称的属性

我得到这个奇怪的错误…我的数据库中的主键是’DocumentID’,所以我知道这不是问题.我正在尝试选择,编辑和删除gridview按钮工作,但我需要正确设置datakeynames以供他们使用.有任何想法吗?
<asp:GridView ID="GridView1" runat="server" DataSourceID="sdsDocuments" EnableModelValidation="True"
        Selectedindex="0" OnSelectedindexChanged="GridView1_SelectedindexChanged" DataKeyNames="DocumentID,DocumentTitle,DocumentBody">
        <Columns>
            <asp:BoundField datafield="DocumentID" HeaderText="DocumentID" ReadOnly="True" SortExpression="ID" />
            <asp:BoundField datafield="DocumentTitle" HeaderText="DocumentTitle" SortExpression="Title" />
            <asp:BoundField datafield="DocumentBody" HeaderText="DocumentBody" SortExpression="Body" />
            <asp:CommandField ShowSelectButton="True" ShowDeleteButton="True" />
        </Columns>
    </asp:GridView>
    <asp:sqlDataSource ID="sdsDocuments" runat="server" ConnectionString="<%$ConnectionStrings:blcDocumentationConnectionString %>"
        SelectCommand="SELECT [DocumentTitle],[DocumentBody] FROM [tblDocument]" />

这是堆栈跟踪…

[HttpException (0x80004005): DataBinding: 'System.Data.DaTarowView' does not contain a                 property with the name 'DocumentID'.] 
        System.Web.UI.DataBinder.GetPropertyValue(Object container,String propName) +8672869
       System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource,Boolean dataBinding) +2178
       System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +57
       System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +14
       System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +114
       System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments,DataSourceViewSelectCallback callback) +31
       System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
       System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
       System.Web.UI.WebControls.GridView.DataBind() +4
       System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
       System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +72
       System.Web.UI.Control.EnsureChildControls() +87
       System.Web.UI.Control.PreRenderRecursiveInternal() +44
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171

   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterasyncPoint) +842

解决方法

那么你没有选择documentid列,因此它不存在于datatable或dataview中,它们绑定到grid或通过datatable引用该列.

将您的查询更改为

SelectCommand="SELECT [DocumentID],[DocumentTitle],[DocumentBody] FROM [tblDocument]" />

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....