注册 登录
编程论坛 C# 论坛

怎么点击dataGridView1内嵌的按钮删除一行数据呀,搞了一天没搞好、、

shengdong521 发布于 2016-03-07 22:59, 4752 次点击
只有本站会员才能查看附件,请 登录

程序代码:
   private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if(dataGridView1.Columns[e.ColumnIndex].Name == "删除")
            {
                SqlConnection connect = new SqlConnection(@"Data Source=localhost;Database=controlcenter;user id=sa;password=sa123;");
                SqlCommand command = new SqlCommand();
                command.Connection = connect;
                connect.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataSet dataset1 = new DataSet();


               
            }

不是新添加的按钮,是dataGridView1中添加的DataGridViewButtonColumn按钮。求大神指点,我弄了好久弄不好
6 回复
#2
shengdong5212016-03-08 11:12
会的帮下忙吧,谢谢了!!!!!!!!!!!!!!!!!!!!
#3
over12302016-03-10 11:34
点击按钮后,获取本行的某个(些)内容后,再用sql 删除
#4
over12302016-03-10 11:37
            if (((DataGridView)sender).Columns[e.ColumnIndex].Name == "删除")
            {
                MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()); //获取点击行第一列的内容
            }

后面再用SQL的删除语句,如果你的序号不是唯一的,就用后面其他字段

[此贴子已经被作者于2016-3-10 11:38编辑过]

#5
shengdong5212016-03-10 15:01
回复 4楼 over1230
我就是写不好删除的指令呀。。。教下我吧
#6
over12302016-03-11 16:21
"delete from 表名 where 名称= '"+dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()+"'"
#7
maxke2016-08-31 16:29
只有本站会员才能查看附件,请 登录
希望能有所帮助。
1