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.
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.
/// 当复选框被点击时发生
/// </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;
}
}
}
/// 删除所选记录
/// </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();
}
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();
加载到checkItem 里完后找selectItems就可了遍历出来啦;
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();
错误 5 “System.Windows.Forms.CheckedListBox.ObjectCollection”并不包含“Items”的定义
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 );
}