DropDownList按照Gridview获取数据获取到的是定义格式

首先需要把DropDownList改成允许服务器返回。

然后绑定的时候需要以下两项。

DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "name";

完整例子

using System.Data;
using System.Data.OracleClient;

string str = "Data Source=127.0.0.1/xkp;User ID=jsb;PassWord=jsb";
string sql = "select name from userlist";
OracleConnection conn = new OracleConnection(str);
OracleDataAdapter dr = new OracleDataAdapter(sql, conn);
DataSet ds = new DataSet();//创建数据集;
dr.Fill(ds); //填充数据集
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "name";
DropDownList1.DataBind();
if (conn.State == ConnectionState.Open) //判断数据库连接状态,是否连接
{
conn.Close();
}

猜你喜欢

转载自www.cnblogs.com/yangsongyan/p/9029167.html