DropDownList等的数据绑定
用以下形式绑定
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[ "bookstoreConnectionString "].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
conn.Open();
string sql = "select sortID from sort ";
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
sda.Fill(ds);
ListBox1.DataSource = ds;
ListBox1.DataBind();
DropDownList1.DataSource = ds;
DropDownList1.DataBind();
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
}
}
================
但运行后DropDownList和ListBox显示的数据是“System.Data.DataRowView”,GridView是正常的
怎样使DropDownList实现绑定?
------解决方案-------------------- DropDownList1.DataSource = ds.Table[0].DefaultView;
DropDownList1.DataValueField = "sortID ";
DropDownList1.DataTextField = "sortID ";
DropDownList1.DataBind();
------解决方案--------------------ListBox1.DataTextField = "字段名 "; //绑定到Text的字段
ListBox1.DataValueField = "字段名 "; //绑定到Value的字段
ListBox1.DataSource = ds;
ListBox1.DataBind();
------解决方案-------------------- DropDownList1.DataSource = 数据集
DropDownList1.DataValueField = "列名 ";
DropDownList1.DataTextField = "名 ";
DropDownList1.DataBind();