DataGridView批量删除
要实现如图所示的批量删除:
.gif)
第一列为程序动态添加的CheckBox列,关键的两个函数为:
1 数据填充函数:
2 删除按钮触发事件:
.gif)
第一列为程序动态添加的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();
}
{
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();
}
}
{
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();
}
}
上一篇:winform DES加解密
下一篇:winform TextBox焦点插入位置并取得选中文字
![]() | 2016/6/13 19:45:00 |
This indterucos a pleasingly rational point of view. | |
![]() | |
![]() | 2011/6/9 11:13:00 |
谢谢分享。 | |
![]() |
申明:本站部分文章来自网络,由于各种原因对文章的来源无从考究,如果您是“
DataGridView批量删除
”的原作者,若侵犯您的版权,请与我联系!在此请您原谅我的幼稚和无知!联系方法:email:ahuinan@21cn.com QQ:106494262
感谢以下网友对网站提出的建议:
1、感谢“蓝树叶kiss”网友发现一个评论漏洞。(2009-2-28)
2、感谢“陈臣”对程序优化和seo方面的建议。(2009-3-18)
感谢以下网友对网站提出的建议:
1、感谢“蓝树叶kiss”网友发现一个评论漏洞。(2009-2-28)
2、感谢“陈臣”对程序优化和seo方面的建议。(2009-3-18)