asp.net 数据绑定:“System.Data.DataRowView”不包含名为

oalqel3c  于 2023-08-08  发布在  .NET
关注(0)|答案(5)|浏览(190)

我收到一个奇怪的错误...我的数据库中的主键是“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

dgjrabp2

dgjrabp21#

您没有选择documentid列,因此它不存在于您绑定到网格或通过datatable引用该列的datatable或dataview中。
将查询更改为

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

字符串

wh6knrhe

wh6knrhe2#

1-第一步,必须在SELECT语句中选择Documentid列

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

字符串
2-你必须像以前那样将它包含在datagridview COLUMN中:

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


3-如果您在datagridview中使用Paging AllowPaging=“True”,则必须在事件OnSelectedIndexChanged=“GridView1_SelectedIndexChanged”上选择documentid以选择第二页和第三页中的数据,依此类推:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataSourceID="sdsDocuments" EnableModelValidation="True"
        SelectedIndex="0" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" DataKeyNames="DocumentID, DocumentTitle, DocumentBody">


然后在事件OnSelectedIndexChanged=“GridView1_SelectedIndexChanged”上:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
                DataTable dt = // your SELECT statement //
                GridView1.DataSource = dt;
                GridView1.PageIndex = e.NewPageIndex;
                GridView1.DataBind();
}


如果您不使用此void,那么当您单击第2页时,您将得到相同错误

zengzsys

zengzsys3#

看起来您的sqldatasource没有返回DocumentId列。
也许你应该将datasource定义改为:

<asp:SqlDataSource ID="sdsDocuments" runat="server" ConnectionString="<%$ ConnectionStrings:blcDocumentationConnectionString %>"
    SelectCommand="SELECT [DocumentID], [DocumentTitle], [DocumentBody] FROM [tblDocument]" />

字符串

ckocjqey

ckocjqey4#

DataSet Année = new DataSet();
Année.ReadXml(Server.MapPath("~/Annees.xml"));

DropDownList1.DataTextField = "Number";
DropDownList1.DataValueField = "Number";
DropDownList1.DataSource = "Année";

DropDownList1.DataBind();

字符串

gdx19jrr

gdx19jrr5#

1.通常是由于列名拼写错误而发生的,因此应该重新检查。
1.如果将DataKeyNames属性指定为“Sr.而实际列名是“Sr.否”,因此会抛出上述错误。

相关问题