将控件绑定到 LINQ 查询 - 找不到字段
Binding controls to LINQ queries - cannot find field
我尝试将多个控件绑定到 LINQ 查询,但出现以下错误:
$exception {"DataBinding: 'System.Data.DataRow' does not contain a property with the name 'Key'."} System.Exception
{System.Web.HttpException}
我按以下方式绑定它:
myDropDownList.DataSource = myDataTable.AsEnumerable().Where(
r => ((string) r["ColumnName"]) == "ColumnIWant").ToList()
myDropDownList.DataTextField = "Key";
myDropDownList.DataValueField = "Value";
我已经尝试过使用和不使用 .ToList(),正如其他答案中所建议的那样,但没有效果。
"myDataTable"
有两列 "Key"
和 "Value"
。据我了解,您可以通过这种方式进行绑定,但我似乎缺少指定 属性 名称的步骤。
如果它在数据 table 中确实包含 key
和 Value
的值,那么我建议您像下面这样使用:
myDropDownList.DataSource = myDataTable.AsEnumerable().Where(
r => (r.Field<string>("ColumnName")) == "ColumnIWant").ToList<DataRow>();
myDropDownList.DataTextField = "Key";
myDropDownList.DataValueField = "Value";
我已将 ToList()
更改为 .ToList<DataRow>();
,以便数据源可以像从数据 table 中一样找到 key
和 value
,并且像这样更改比较:r.Field<string>("ColumnName")
我的建议(因为它以这种方式使用 GridView 和 DropDownList 控件对我起作用)是在设计文件 (aspx) 中指定此 DataTextField
和 DataValueField
属性,如下所示:
<asp:DropDownList ID="DropDownList1" runat="server"
DataTextField="Key" DataValueField="Value">
</asp:DropDownList>
这样,您仍然可以将 ToList()
函数应用于数据表,它会起作用(已测试)。
如果没有,也许您需要在填满 myDataTable
后设置一个断点,在 LINQ 查询后设置另一个断点,以检查 "Key" 和 "Value" 列仍然存在。
使用 .CopyToDataTable() 适用于我正在尝试做的事情,但如果可能的话,我仍然更愿意知道如何将数据直接绑定到 EnumerableRows 集合。
这是我所做的:
myDropDownList.DataSource = myDataTable.AsEnumerable().Where(
r => ((string) r["ColumnName"]) == "ColumnIWant").CopyToDataTable();
myDropDownList.DataTextField = "Key";
myDropDownList.DataValueField = "Value";
我尝试将多个控件绑定到 LINQ 查询,但出现以下错误:
$exception {"DataBinding: 'System.Data.DataRow' does not contain a property with the name 'Key'."} System.Exception {System.Web.HttpException}
我按以下方式绑定它:
myDropDownList.DataSource = myDataTable.AsEnumerable().Where(
r => ((string) r["ColumnName"]) == "ColumnIWant").ToList()
myDropDownList.DataTextField = "Key";
myDropDownList.DataValueField = "Value";
我已经尝试过使用和不使用 .ToList(),正如其他答案中所建议的那样,但没有效果。
"myDataTable"
有两列 "Key"
和 "Value"
。据我了解,您可以通过这种方式进行绑定,但我似乎缺少指定 属性 名称的步骤。
如果它在数据 table 中确实包含 key
和 Value
的值,那么我建议您像下面这样使用:
myDropDownList.DataSource = myDataTable.AsEnumerable().Where(
r => (r.Field<string>("ColumnName")) == "ColumnIWant").ToList<DataRow>();
myDropDownList.DataTextField = "Key";
myDropDownList.DataValueField = "Value";
我已将 ToList()
更改为 .ToList<DataRow>();
,以便数据源可以像从数据 table 中一样找到 key
和 value
,并且像这样更改比较:r.Field<string>("ColumnName")
我的建议(因为它以这种方式使用 GridView 和 DropDownList 控件对我起作用)是在设计文件 (aspx) 中指定此 DataTextField
和 DataValueField
属性,如下所示:
<asp:DropDownList ID="DropDownList1" runat="server"
DataTextField="Key" DataValueField="Value">
</asp:DropDownList>
这样,您仍然可以将 ToList()
函数应用于数据表,它会起作用(已测试)。
如果没有,也许您需要在填满 myDataTable
后设置一个断点,在 LINQ 查询后设置另一个断点,以检查 "Key" 和 "Value" 列仍然存在。
使用 .CopyToDataTable() 适用于我正在尝试做的事情,但如果可能的话,我仍然更愿意知道如何将数据直接绑定到 EnumerableRows 集合。
这是我所做的:
myDropDownList.DataSource = myDataTable.AsEnumerable().Where(
r => ((string) r["ColumnName"]) == "ColumnIWant").CopyToDataTable();
myDropDownList.DataTextField = "Key";
myDropDownList.DataValueField = "Value";