c#从数据库绑定数据到dropdownlist

来源:百度知道 编辑:UC知道 时间:2024/05/17 20:48:28
做个dropdownlist,数据从数据库里娶我用
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
myConnection = new SqlConnection("server=.;database=FourseasJobs;user id=sa;password=");
SqlDataAdapter myCommand=new SqlDataAdapter(" select jobkinds from myjobs_jobkinds ",myConnection);
DataSet ds=new DataSet();
myCommand.Fill(ds);
DataView source=new DataView(ds.Tables[0]);
DropDownList1.DataSource=source;
DropDownList1.DataBind();
}
这个方法去绑定.运行起来DropDownList1可以下拉了.但是从数据库里的值都是 System.Data.DataRowView这个呢?那位大哥指点下.我做的绑定错了吗.如果没错为什么显示的值不是数据库的值.而都是
System.Data.DataRowView这个呢?郁闷中..

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
myConnection = new SqlConnection("server=.;database=FourseasJobs;user id=sa;password=");
SqlDataAdapter myCommand=new SqlDataAdapter(" select jobkinds from myjobs_jobkinds ",myConnection);
DataSet ds=new DataSet();
myCommand.Fill(ds);
DataView source=new DataView(ds.Tables[0]);
DropDownList1.DataTextFiled('列名'); //此列名为DropDownList1显示的值
DropDownList1.DataValueFiled('列名');
DropDownList1.DataSource=source;
DropDownList1.DataBind();
}

估计是有2个属性没有设置,你看看TEXT,VALUE的属性你把字段设置一下就好了啊

后面写错了,
dropdownlist.datasource=ds;
dropdownlist.datatextfiled="绑定的列"//关键地方,
dropdownlist.databind();

试一下,
private void Page_Load(object sender, System.EventArgs e)
{
myConnection = new SqlConnection("server=.;database=FourseasJobs;user id=sa;password=");
Sql