checkedListBox绑定数据成功.                checkedListBox1.DataSource = ds.Tables["result"];
                checkedListBox1.DisplayMember = "courname";
                checkedListBox1.ValueMember = "courid";复选框选中想要删除的行.使用循环语句来执行删除功能.
           for (int s = 0; s < this.checkedListBox1.CheckedItems.Count; s++)
            {
                courid = this.checkedListBox1.CheckedItems[s].ToString();
                SqlCommand cmd = new SqlCommand("delete from s_SelectResult where studid ='" + stud + "' and courid = '" + courid + "'", con.conn);
                con.conn.Open();
                cmd.ExecuteNonQuery();
                con.conn.Close();
            }红色字体部分.我想得到被选中的行的值,就是绑定的时候绑定的courid.

解决方案 »

  1.   

    courid = this.checkedListBox1.CheckedItems[s].Tag.ToString(); 应该是这样写的吧,好像是有个Tag来的,你找找是不...
      

  2.   

    courid = this.checkedListBox1.SelectedValue.ToString(); 
      

  3.   

     /// <summary>
        /// 当复选框被点击时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                CheckBox cbox = (CheckBox)(GridView1.Rows[i].FindControl("CheckBox1"));
                if (CheckBox2.Checked == true)
                {
                    cbox.Checked = true;
                }
                else
                {
                    cbox.Checked = false;
                }
            }
        }
      

  4.   

    /// <summary>
        /// 删除所选记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
                if (cbox.Checked == true)
                {
                    Common.ExecuteSql("delete from Employee where ID=" + Convert.ToUInt32(GridView1.DataKeys[i].Value) + "");
                }
            }
            bind();
        }
      

  5.   

    int i;
    string s=String.Empty;
    for (i = 0; i <= (checkedListBox1.Items.Count-1); i++)
    {
       if (checkedListBox1.GetItemChecked(i))
       {
          s = ",'"+checkedListBox1.Items.Items[i].ToString()+"'"+s;
       }
    }
    s=s.SubString(1);//去掉前面的','
    SqlCommand cmd = new SqlCommand("delete from s_SelectResult where studid ='" + stud + "' and courid in(" + courid + ")", con.conn);
    con.conn.Open();
    cmd.ExecuteNonQuery();
    con.conn.Close(); 
      

  6.   

    我建议你加载的时候把"courname","courid"放在一个类里Item.add(new className("courname","courid"));
    加载到checkItem 里完后找selectItems就可了遍历出来啦; 
      

  7.   

    修改一下:int i;
    string s=String.Empty;
    for (i = 0; i <= (checkedListBox1.Items.Count-1); i++)
    {
      if (checkedListBox1.GetItemChecked(i))
      {
          s = ",'"+checkedListBox1.Items.Items[i].ToString()+"'"+s;
      }
    }
    s=s.SubString(1);//去掉前面的','
    SqlCommand cmd = new SqlCommand("delete from s_SelectResult where studid ='" + stud + "' and courid in(" +s+ ")", con.conn);
    con.conn.Open();
    cmd.ExecuteNonQuery();
    con.conn.Close(); 
      

  8.   

    遍历所有Item,根据GetItemChecked(index)可以判断是否选中
      

  9.   

    s = ",'"+checkedListBox1.Items.Items[i].ToString()+"'"+s;
    错误 5 “System.Windows.Forms.CheckedListBox.ObjectCollection”并不包含“Items”的定义
      

  10.   

    已经搞定,自己查了MSDN解决的.            string stud = this.listBox1.SelectedValue.ToString();
                string courid = string.Empty;
                string s = string.Empty;
                if (checkedListBox1.CheckedItems.Count != 0)
                {
                    // If so, loop through all checked items and print results.
                    
                    for (int x = 0; x <= checkedListBox1.CheckedItems.Count - 1; x++)
                    {
                        s = checkedListBox1.Items[x].ToString();
                        s = s.Substring(0,6);
                        courid = courid + "," + s ;
                    }
                    courid = courid.Substring(1);
                    SqlCommand cmd = new SqlCommand("delete from s_SelectResult where studid ='" + stud + "' and courid in (" + courid + ")", con.conn);
                    con.conn.Open();
                    cmd.ExecuteNonQuery();
                    con.conn.Close();
                    MessageBox.Show("删除完毕.");
                    this.checklist( stud );
                }
      

  11.   

    楼主 我的 checkedListBox1.Items[x]为什么获得的都是"System.Data.DataRowView"这个啊?
      

  12.   

    楼主 我的 checkedListBox1.Items[x]为什么获得的都是"System.Data.DataRowView"这个啊?