简体   繁体   English

将控件绑定到LINQ查询-找不到字段

[英]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)中指定此DataTextFieldDataValueField属性,如下所示:

<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 : 如果它在数据表中明确包含keyValue ,那么我建议您使用如下所示:

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") 这样数据源就可以从数据表中找到keyvalue ,并像这样更改比较: 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.

相关问题 无法使用空间 LINQ 查询在多边形内找到多边形 - Cannot find polygons within polygons using spatial LINQ queries DataGridView没有绑定到我的linq查询 - DataGridView not binding to my linq queries 找不到 Xamarin 控件 - Cannot find Xamarin controls 无法找到引用绑定的源'RelativeSource FindAncestor,AncestorType ='MahApps.Metro.Controls.Glow',AncestorLevel ='1'' - Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='MahApps.Metro.Controls.Glow', AncestorLevel='1'' 找不到参考&#39;RelativeSource FindAncestor,AncestorType =&#39;System.Windows.Controls.UserControl&#39;,AncestorLevel =&#39;1&#39;&#39;的绑定源 - Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1'' 找不到绑定源 - Cannot find source for binding 在页面中找不到Checkbox控件 - Cannot find Checkbox controls in page 在RavenDB LINQ查询中使用日期字段比较 - Using date field comparisons in RavenDB LINQ queries LINQ to SQL找到字段的平均值? - LINQ to SQL find average of field? WPF绑定无法找到与引用绑定的源 - WPF Binding Cannot find source for binding with reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM