我收到这个奇怪的错误......我的数据库中的主键是'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
答案 0 :(得分:7)
好吧,你没有选择documentid
列,因此它不存在于数据表或数据视图中,你绑定到网格或通过数据表引用该列。
将您的查询更改为
SelectCommand="SELECT [DocumentID],[DocumentTitle], [DocumentBody] FROM [tblDocument]" />
答案 1 :(得分:0)
您的sqldatasource似乎没有返回DocumentId列。
也许您应该将数据源定义更改为:
<asp:SqlDataSource ID="sdsDocuments" runat="server" ConnectionString="<%$ ConnectionStrings:blcDocumentationConnectionString %>"
SelectCommand="SELECT [DocumentID], [DocumentTitle], [DocumentBody] FROM [tblDocument]" />
答案 2 :(得分:0)
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 AllowPaging =“ True”中使用分页,则还必须在事件OnSelectedIndexChanged =“ GridView1_SelectedIndexChanged”上同时选择文件ID,以在第二页和第三页中选择数据,依此类推:
<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();
}
如果您不使用此空白,则单击第2页时会出现相同的错误
答案 3 :(得分:0)
DataSet Année = new DataSet();
Année.ReadXml(Server.MapPath("~/Annees.xml"));
DropDownList1.DataTextField = "Number";
DropDownList1.DataValueField = "Number";
DropDownList1.DataSource = "Année";
DropDownList1.DataBind();