DropDownList 数据绑定

绑定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();

转载于:https://www.cnblogs.com/EasyData/archive/2007/10/11/920950.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值