C#中 下拉框如何绑定数据表?

来源:百度知道 编辑:UC知道 时间:2024/09/25 13:25:36

dropdownlist1.DataSource=DataTable1.DefaultView;
dropdownlist1.DataBind();

方法一

SqlDataAdapter da = new SqlDataAdapter("select id,name from test",conn);
DataSet ds=new DataSet();
da.Fill(ds,"test");

dropdownlist1.DataSource=ds.Tables[0];
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "id";
dropdownlist1.DataBind();

方法二

for(int i=0;i<ds.Tables[0].Count;i++)
{
dropdownlist1.Items.Add(ds.Tables[0].Rows[i][0]);
}

SqlCommand cmd1 = new SqlCommand("select c_name from fdy where department='" + comboBox1.Text + "'", conn);
SqlDataReader dr1 = cmd1.ExecuteReader();
comboBox3.Items.Clear();
while (dr1.Read())
comboBox3.Items.Add(dr1.GetString(0).Trim());

好象只要点就行了,,不需要写代码