| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3422 人关注过本帖
标题:无法删除datagridview中选定行(数据库中删除)
只看楼主 加入收藏
lukebc
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:3
帖 子:74
专家分:32
注 册:2009-8-18
结帖率:71.43%
收藏
 问题点数:0 回复次数:5 
无法删除datagridview中选定行(数据库中删除)
public static string str = "";
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count > 0)
                str = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除吗?", "提示", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                if (str == "")
                {
                    MessageBox.Show("请选择你要删除的行!", "警告", MessageBoxButtons.OKCancel);
                    return;
                }
                string sql = "delete from text1 where bh='" + str + "'";
                str = "";
                MessageBox.Show("该学生已被删除!", "提示");
            }
str 没有取得值,一直弹出“请选择你要删除的行”,不知道怎么改
搜索更多相关主题的帖子: private public 数据库 
2016-08-30 15:15
over1230
Rank: 8Rank: 8
等 级:贵宾
威 望:25
帖 子:113
专家分:897
注 册:2009-10-9
收藏
得分:0 
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count > 0)
                str = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
        }

改成
 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
             if (this.dataGridView1.SelectedRows.Count > 0)
                str = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
        }

设中断,监控下程序运行就情况,很容易分析出问题原因
2016-08-30 16:34
向洪林
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:重庆市奉节县
等 级:版主
威 望:9
帖 子:118
专家分:405
注 册:2015-3-10
收藏
得分:0 
与数据库有交互吗??

新手上路,望大家多多照顾,谢谢!
2016-08-31 09:23
maxke
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-8-11
收藏
得分:0 
希望能有所帮助
 private void dgv_cx_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                //条件为真
                if (e.ColumnIndex == 0)
                {
                    DataGridViewCellCollection dgvCC = this.dgv_cx.Rows[e.RowIndex].Cells;
                    DBaccess.AccessHelper.ExecuteSql("UPDATE 成型浇注 SET 浇注日期='" + dgvCC["浇注日期"].Value + "',料浆编号='" + dgvCC["料浆编号"].Value + "',模具编号='" + dgvCC["模具编号"].Value + "',模盖编号='" + dgvCC["模盖编号"].Value + "',成型位置='" + dgvCC["成型位置"].Value + "',浇注面='" + dgvCC["浇注面"].Value + "',浇注时间='" + dgvCC["浇注时间"].Value + "',开模时间='" + dgvCC["开模时间"].Value + "',耗时='" + dgvCC["耗时"].Value + "' WHERE 产品编号='" + this.dgv_cx.Rows[e.RowIndex].Cells["产品编号"].Value + "'");
                    MessageBox.Show("修改成功!");
                }
                //条件为假
                else if (e.ColumnIndex == 1)
                {
                    DialogResult dr = MessageBox.Show("是否要删除此条信息?", "对话框标题", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dr == DialogResult.OK)
                    {
                        DBaccess.AccessHelper.ExecuteSql("DELETE FROM 成型浇注 WHERE 产品编号='" + this.dgv_cx.Rows[e.RowIndex].Cells["产品编号"].Value + "' ");
                        MessageBox.Show("删除成功!");
                        DBaccess.AccessHelper.ExecuteSql("DELETE FROM 产品信息表 WHERE 产品编号='" + this.dgv_cx.Rows[e.RowIndex].Cells["产品编号"].Value + "' ");
                        MessageBox.Show("产品形态已更新!");
                        xianshishuj();
                    }
                    else { MessageBox.Show("请谨慎操作,信息一旦删除会造成数据库漏洞,可以尝试修改数据!"); }
                }
            }
            else
            {
                MessageBox.Show("操作有误!");
            }
        }
2016-09-01 09:09
向洪林
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:重庆市奉节县
等 级:版主
威 望:9
帖 子:118
专家分:405
注 册:2015-3-10
收藏
得分:0 
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvFoods.SelectedRows.Count > 0)//判断GetData是否选中某条信息
            {
                for (int i = 0; i < dgvFoods.SelectedRows.Count; i++)
                {
                    int id = Convert.ToInt32(dgvFoods.SelectedRows[i].Cells[0].Value);//获取ID
                    dgvFoods.Rows.RemoveAt(dgvFoods.SelectedRows[i].Index);
                    //string names = dgvFoods.SelectedCells[0].Value.ToString();//获取欲删除信息的名称
                    string connectionString = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
                    SqlConnection conn = new SqlConnection(connectionString);
                    conn.Open();
                    string sql = "delete from tb_GuestFood where ID='" + id + "' and zhuotai='" + RName + "'";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                MessageBox.Show("移除成功");
                GetData();//重新绑定数据
            }
            else
            {
                MessageBox.Show("请您选择不需要的餐饮,然后删除");
            }
        }

希望对你能有所帮助

新手上路,望大家多多照顾,谢谢!
2016-09-01 22:59
q8021125
Rank: 2
等 级:论坛游民
帖 子:8
专家分:10
注 册:2017-3-20
收藏
得分:0 
  
        private void button2_Click(object sender, EventArgs e)
        {
        int   str=-1;//建立变量
        try//
        {
            str= dataGridView1.CurrentRow.Index;//获取当前行索引
        }
        Exception()
        {
             MessageBox.Show("请选择你要删除的行或表格内没有数据!", "警告", MessageBoxButtons.OKCancel);
             return;
        }           
        DialogResult dr = MessageBox.Show("确定要删除吗?", "提示", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {               
                string sql = "delete from text1 where bh='" + dataGridView1.Rows[str].Cells[0].Value.ToString() + "'";
                str = "";
                MessageBox.Show("该学生已被删除!", "提示");
            }

      点击删除时,代码意思是,先获取该行索引号,如果获取不到,弹出没有选择行的提示,如果有索引号,将进入删除选择,是,进行删除,否跳过

[此贴子已经被作者于2017-3-20 12:00编辑过]

2017-03-20 11:52
快速回复:无法删除datagridview中选定行(数据库中删除)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021635 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved