Chinaunix首页 | 论坛 | 博客
  • 博客访问: 236491
  • 博文数量: 59
  • 博客积分: 2016
  • 博客等级: 大尉
  • 技术积分: 660
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-04 17:30
文章分类
文章存档

2013年(1)

2011年(2)

2010年(7)

2009年(30)

2008年(19)

我的朋友

分类: C/C++

2009-09-18 21:28:17

要实现如图所示的批量删除:


第一列为程序动态添加的CheckBox列,关键的两个函数为:
1 数据填充函数:
private void fillIt()
{
dataGridView1.Columns.Clear();
SqlConnection conn = new SqlConnection("server=.;database=daily;uid=sa;pwd=123456");
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = conn;
da.SelectCommand.CommandType = CommandType.Text;
da.SelectCommand.CommandText = "select * from sceret";
da.Fill(ds,"sceret");
dataGridView1.DataSource = ds.Tables["sceret"];
DataGridViewCheckBoxColumn newColumn = new DataGridViewCheckBoxColumn();
dataGridView1.Columns.Insert(0,newColumn);
conn.Close();
}

2 删除按钮触发事件:
private void btnDel_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("server=.;database=daily;uid=sa;pwd=123456");
conn.Open();
string delStr = "(";
for (int i = 0; i < Convert.ToInt32(dataGridView1.RowCount); i++)
{
if (Convert.ToBoolean(dataGridView1[0, i].Value))
{
delStr += dataGridView1[1, i].Value.ToString() + ",";
}
}
if (delStr == "(")
{
MessageBox.Show("没有需要删除的项");
}
else
{
delStr = delStr.Substring(0, delStr.Length - 1) + ")";
delStr = "delete sceret where id in" + delStr;
SqlCommand cmd = new SqlCommand(delStr, conn);
cmd.ExecuteNonQuery();
fillIt();
}

}


转载:

阅读(1185) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-03-05 11:06:03

请教高手一个问题:如何在dataGridView中设置列的子列,比如一个列的下面含有三个子列,这种情况可行吗?可以的话麻烦给出代码,不胜感激,谢谢!