使用DataRow数组绑定DropDownList

时间:2011-06-29 10:24:45

标签: c# asp.net data-binding drop-down-menu datarow

我正在将DropDownListDataRow绑定,但它会抛出异常:

  

DataBinding:'System.Data.DataRow'不包含名为'CourseEligiblility'的属性。

代码:

DataRow[] dRow = dt.Select("CourseID=" + ddlCourse.SelectedValue);
ddlBeStream.DataTextField = "CourseEligiblility";
ddlBeStream.DataValueField = "ID";
ddlBeStream.DataSource = dRow;
ddlBeStream.DataBind();

5 个答案:

答案 0 :(得分:5)

只需使用将DataRow Array复制到新DataTable的函数并将其返回。

//  .
//  .
//  .
ddlBeStream.DataSource = dRow.CopyToDataTable;
ddlBeStream.DataBind();

答案 1 :(得分:4)

获取DataView并按字段

过滤

例如。

DataView dv = dt.DefaultView;
dv.RowFilter = "colums=1";
DropDownList1.DataSource = dv;
DropDownList1.DataTextField = "colName";
DropDownList1.DataValueField = "colID";
DropDownList1.DataBind();

答案 2 :(得分:1)

尝试using ddlBeStream.DataBind();

答案 3 :(得分:0)

我认为你写错了字段的名字。请尝试以下方法:

ddlBeStream.DataTextField = "CourseEligibility";

答案 4 :(得分:0)

对于仍在使用旧版ASP.net的人:

DataRow[] dRow = dt.Select("CourseID=" + ddlCourse.SelectedValue);
//Copy Datatable and import found rows
System.Data.DataTable dt_Temp = dt.Clone();    
foreach (System.Data.DataRow dr in drRow) { dt_Temp.ImportRow(dr); }

ddlBeStream.DataTextField = "CourseEligiblility";
ddlBeStream.DataValueField = "ID";
//Use new DataTable
ddlBeStream.DataSource = dt_Temp;
ddlBeStream.DataBind();