急!!!!!!如何将数据源的一列数据绑定到Dropdownlist的项中?

来源:百度知道 编辑:UC知道 时间:2024/06/25 03:28:16
public void DropDownList1_show()
{
string strConn = "Data Source=127.0.0.1;Initial Catalog=ABIS_FOOD;Persist Security Info=True;User ID=sa;Password=123456";
string strSql = "select * from t_sys_data WHERE DataCode='CPLB'";
SqlDataAdapter da = new SqlDataAdapter(strSql, strConn);
DataSet ds = new DataSet();
da.Fill(ds, "t_sys_data");
DropDownList1.DataSource = ds.Tables["DataMc"];
DropDownList1.DataTextField = ds.Tables[0].Columns[1].ColumnName;
DropDownList1.DataValueField = ds.Tables[0].Columns[0].ColumnName;
DropDownList1.DataBind();
//DropDownList1.Items.Insert(0, new ListItem("请选择", "0"));

}
public void GridView2_show()
{
string strConn = "Data Source=127.0.0.1;Initial Catalog=ABIS_FOOD;Persist Security Info=True;User ID=sa;

用dataset来绑定这个控件的话太浪费了,直接用读取的方式吧:

public void binddrop()
{
string ConnString = ConfigurationSettings.AppSettings["Finance"];连接字串
SqlConnection Conn = new SqlConnection(ConnString);
string SQL_Select = "select type from finance ";
SqlCommand myCommand = new SqlCommand(SQL_Select, Conn);

myCommand.Connection.Open();

SqlDataReader myReader = myCommand.ExecuteReader();

while ( myReader.Read() )

{

DropDownList1.Items.Add(new ListItem(myReader["Type"].ToString())), myReader["id"].ToString()));//增加Item

//或者这样也能绑定,

//DropDownList1.Items.Add( new ListItem( myReader[1].ToString(),myReader[0].ToString() ) );//增加Item

}
myCommand.Connection.Close();
}