C# CheckboxList应用

小例子,不多说明,实现CheckBox 文本值,并判断是不断选中
ExpandedBlockStart.gif 代码
  private   string  conn = @" Data Source=.;Initial Catalog=test1;Integrated Security=false;uid=sa;Password=guo " ;
        
public  Form1()
        {
            InitializeComponent();
        }

        
private   void  Form1_Load( object  sender, EventArgs e)
        {


            databind();

        }

        
private   void  button1_Click( object  sender, EventArgs e)
        {
            ArrayList ar
= new  ArrayList();
            
for  ( int  i  =   0 ; i  <  checkedListBox1.Items.Count; i ++ )
            {
                
if  (checkedListBox1.GetItemChecked(i))
                    ar.Add(checkedListBox1.GetItemText(checkedListBox1.Items[i].ToString().Substring(
0 , checkedListBox1.Items[i].ToString().IndexOf( " . " ))));
            }
            
// MessageBox.Show(ar.Count.ToString());
            SqlConnection sqlcon  =   new  SqlConnection(conn);
            
string  sql  =   " delete from item where UserID=1 " ;
            SqlCommand sqlcom 
=   new  SqlCommand(sql, sqlcon);
            
if  (sqlcon.State  ==  ConnectionState.Closed)
                sqlcon.Open();
            
int  id  =  sqlcom.ExecuteNonQuery();
            
for  ( int  i  =   0 ; i  <  ar.Count; i ++ )
            {
                
// MessageBox.Show(ar[i].ToString());
                 string  sql1  =   " insert into item(UserID,RoleID) values(1, " + ar[i].ToString() + " ) " ;
                SqlCommand sqlcom1 
=   new  SqlCommand(sql1, sqlcon);
                
if  (sqlcon.State  ==  ConnectionState.Closed)
                    sqlcon.Open();
                
int  id1  =  sqlcom1.ExecuteNonQuery();
                sqlcon.Close();
            }
            databind();
        }
        
private   void  databind()
        {
            checkedListBox1.Items.Clear();
            SqlConnection sqlcon 
=   new  SqlConnection(conn);
            
            
string  sql  =   " select * from ItemIn " ;
            SqlCommand sqlcom 
=   new  SqlCommand(sql, sqlcon);
            SqlDataAdapter sqlda 
=   new  SqlDataAdapter(sql, sqlcon);
            sqlda.SelectCommand 
=  sqlcom;
            DataSet ds 
=   new  DataSet();
            
if  (sqlcon.State  ==  ConnectionState.Closed)
                sqlcon.Open();
            sqlda.Fill(ds, 
" dt " );
            sqlcon.Close();
            DataTable dt 
=  ds.Tables[ " dt " ];


            
string  sql2  =   " select * from item where UserID=1 " ;
            SqlCommand sqlcom1 
=   new  SqlCommand(sql2, sqlcon);
            SqlDataAdapter sqlda1 
=   new  SqlDataAdapter(sql2, sqlcon);
            sqlda.SelectCommand 
=  sqlcom1;
            DataSet ds1 
=   new  DataSet();
            
if  (sqlcon.State  ==  ConnectionState.Closed)
                sqlcon.Open();
            sqlda.Fill(ds1, 
" dt1 " );
            sqlcon.Close();
            DataTable dt1 
=  ds1.Tables[ " dt1 " ];
            
for  ( int  j  =   0 ; j  <  dt.Rows.Count; j ++ )
            {
                
                  checkedListBox1.Items.Add(dt.Rows[j][
" WLID " ].ToString()  +   " . "   +  dt.Rows[j][ " WLTimeIn " ].ToString());
               
            }
            
for  ( int  j  =   0 ; j  <  checkedListBox1.Items.Count; j ++ )
            {
                
for  ( int  i  =   0 ; i  < dt1.Rows.Count; i ++ )
                {
                    
if  (checkedListBox1.GetItemText(checkedListBox1.Items[j].ToString().Substring( 0 , checkedListBox1.Items[j].ToString().IndexOf( " . " )))  ==  dt1.Rows[i][ " RoleID " ].ToString())
                        checkedListBox1.SetItemChecked(j, 
true );
                }
            }
            
        }
    }

 

转载于:https://www.cnblogs.com/gjs85/archive/2010/01/13/1646829.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值