下拉设置为System.Data.DataRow,我的代码有什么问题?

时间:2014-07-16 10:13:14

标签: asp.net vb.net drop-down-menu datatable dataset

    System.Data.DataTable dt = new System.Data.DataTable();
    CDbAccess db = new CDbAccess();
    IDbConnection conn = db.GetConnectionInterface();
    conn.Open();
    string str = "select eName from bt_modules";
    IDbCommand cmd = db.GetCommandInterface(str);
    IDbDataAdapter da = db.GetDataAdapterInterface(cmd);
    da.SelectCommand = cmd;
    DataSet ds = new DataSet();
    da.Fill(ds);
    dt = ds.Tables[0];
    DropDownList2.DataSource = dt;
    DropDownList2.DataBind();
    conn.Close();

以上代码将下拉项设置为System.Data.DataRow

如何获得实际值?

2 个答案:

答案 0 :(得分:0)

尝试这样:

 DropDownList2.DataSource = dt;
 DropDownList1.DataTextField = "eName " '// Column Name
 DropDownList2.DataBind();

如果你想使用像这样的值字段

 DropDownList2.DataSource = dt;
 DropDownList1.DataTextField = "eName " '// Column Name
 DropDownList1.DataValueField = "eid" '// Column Name
 DropDownList2.DataBind();

答案 1 :(得分:0)

DropDownList2.DataSource = ds;

DropDownList2.DataTextField = "eName";

DropDownList2.DataBind();

conn.close();

您也可以参考此代码http://www.aspdotnet-suresh.com/2012/10/showbind-data-to-aspnet-dropdownlist.html