[英]Binding controls to LINQ queries - cannot find field
I have several controls that I am trying to bind to LINQ queries but am getting the following error: 我有几个试图绑定到LINQ查询的控件,但出现以下错误:
$exception {"DataBinding: 'System.Data.DataRow' does not contain a property with the name 'Key'."} System.Exception {System.Web.HttpException} $ exception {“ DataBinding:'System.Data.DataRow'不包含名称为'Key'的属性。”} System.Exception {System.Web.HttpException}
I'm binding it the following way: 我通过以下方式绑定它:
myDropDownList.DataSource = myDataTable.AsEnumerable().Where(
r => ((string) r["ColumnName"]) == "ColumnIWant").ToList()
myDropDownList.DataTextField = "Key";
myDropDownList.DataValueField = "Value";
I have tried this both with and without the .ToList(), as suggested in other answers but with no effect. 正如其他答案中所建议的那样,无论是否有.ToList(),我都尝试过此操作,但没有任何效果。
"myDataTable"
has both columns "Key"
and "Value"
. "myDataTable"
具有列"Key"
和"Value"
。 It was my understanding you could bind this way, but I seem to be missing a step in specifying the property names. 据我了解,您可以通过这种方式进行绑定,但是我似乎缺少指定属性名称的步骤。
My suggestion (as it has worked to me this way with GridView and DropDownList controls), is that you specify this DataTextField
and DataValueField
properties in the design file (aspx) like this: 我的建议(因为它已经对GridView和DropDownList控件DataTextField
),是您在设计文件(aspx)中指定此DataTextField
和DataValueField
属性,如下所示:
<asp:DropDownList ID="DropDownList1" runat="server"
DataTextField="Key" DataValueField="Value">
</asp:DropDownList>
This way, you can still apply the ToList()
function to the DataTable and it will work (tested). 这样,您仍然可以将ToList()
函数应用于DataTable,它将起作用(经过测试)。
If if doesn't, maybe you need to set up one break-point after filling up your myDataTable
and another one after the LINQ query, to check if the "Key" and "Value" columns are still there. 如果不是,则可能需要在填满myDataTable
之后设置一个断点,然后在LINQ查询之后设置另一个断点,以检查“ Key”和“ Value”列是否仍然存在。
If it defenitely contains values for key
and Value
in the data table then i suggest you to use like the following : 如果它在数据表中明确包含key
和Value
,那么我建议您使用如下所示:
myDropDownList.DataSource = myDataTable.AsEnumerable().Where(
r => (r.Field<string>("ColumnName")) == "ColumnIWant").ToList<DataRow>();
myDropDownList.DataTextField = "Key";
myDropDownList.DataValueField = "Value";
I had changed ToList()
to .ToList<DataRow>();
我已经将ToList()
更改为.ToList<DataRow>();
so that the datasourse can find the key
and value
as same as from a datatable, and change the comparison like this: r.Field<string>("ColumnName")
这样数据源就可以从数据表中找到key
和value
,并像这样更改比较: r.Field<string>("ColumnName")
Using .CopyToDataTable() works for what I'm trying to do, but I'd still prefer to know how to bind the data directly to the EnumerableRows collection if that's possible. 使用.CopyToDataTable()可以达到我想要的目的,但是我仍然更愿意知道如何将数据直接绑定到EnumerableRows集合。
Here is what I did: 这是我所做的:
myDropDownList.DataSource = myDataTable.AsEnumerable().Where(
r => ((string) r["ColumnName"]) == "ColumnIWant").CopyToDataTable();
myDropDownList.DataTextField = "Key";
myDropDownList.DataValueField = "Value";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.