绑定DataTable
//创建一个SqlConnection
        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=pubs;Integrated Security=True");
        
        
string SQL_Select = "select job_id,job_desc from dbo.jobs order by job_id desc";

        
//构造一个SqlDataAdapter
        SqlDataAdapter myAdapter = new SqlDataAdapter(SQL_Select, conn);

        
try
        {
            
//开始读取数据
            conn.Open();
            
try
            {
                DataSet dataSet 
= new DataSet();
                myAdapter.Fill(dataSet, 
"jobs");

                
//指定DropDownList使用的数据源
                DropDownList1.DataSource = dataSet.Tables["jobs"].DefaultView;

                
//指定DropDownList使用的表里的那些字段
                DropDownList1.DataTextField = "job_desc"//dropdownlist的Text的字段
                DropDownList1.DataValueField = "job_id";//dropdownlist的Value的字段

                DropDownList1.DataBind();
            }
            
catch (Exception ex)
            {
                
throw ex;
            }
            
finally
            {
                conn.Close();
                conn.Dispose();
            }
        }
        
catch (Exception ex)
        {
            
throw ex;
        }

绑定枚举
方法一:可绑定Text和Value.
foreach (int i in Enum.GetValues(typeof(EnumArea)))
        {
            ListItem item 
= new ListItem(Enum.GetName(typeof(EnumArea), i), i.ToString());
            DropDownList1.Items.Add(item);
        }
方法二:只绑定Text.
DropDownList1.DataSource = Enum.GetValues(typeof(EnumArea));
DropDownList1.DataBind();

相关文章:

  • 2021-10-26
  • 2022-12-23
  • 2021-09-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
猜你喜欢
  • 2022-02-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-19
  • 2022-12-23
  • 2021-06-12
相关资源
相似解决方案