C# CheckboxList应用

小例子,不多说明,实现CheckBox 文本值,并判断是不断选中
代码
 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);
                }
            }
            
        }
    }

 

posted @ 2010-01-13 20:20  古史漫谈  阅读(1525)  评论(0编辑  收藏  举报