在C#中dataGridView1如何删除行并删除数据库里面的

OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\学习园地\\C#\\SQL\\db1.mdb");
conn.Open();

int n = dataGridView1.Rows.Count;
for(int i=0;i<n-1;i++)
{
if (dataGridView1.Rows[i].Selected == true)
{
dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
}
}

希望可以快点!急!完成附加20!

1、我们可以在窗体页面上添加ContextMenuStrip快捷菜单。

2、然后我们就能就能在删除行这个子项中编写删除事件代码。

3、此时我们我们就能调用DataGridView的Rows属性。

4、然后引用rowIndex属性即可获得行下标。

5、此时我们就能通过获取到Rows行下标的IsNewRow属性判断是否被选中。

6、如果被选中了,我们就能利用DataGridView对象的事件e的rowIndex属性删除。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-11-16
//按钮删除事件
        private void 删除toop_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确实要删除该行吗?", "询问", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                //获取点击datagridview1的行的 行号
                int r = this.dataGridView1.CurrentRow.Index;
                //获取此行的 员工编号 的值
                string yuangong = this.dataGridView1.Rows[r].Cells["员工号"].Value.ToString();
                //删除 datagridview1 的选中行
                this.dataGridView1.Rows.Remove(this.dataGridView1.Rows[r]);
                //删除数据库的 员工编号 的对应行
                string str = "server=172.24.140.16;database=UnionTrade;User ID=union_admin;Password=Tianjin@2017";  //数据库链接字符串
                SqlConnection conn = new SqlConnection(str);                            //实例化链接
                conn.Open();                        //打了链接
                string sql = "delete from shetuanxinxi where 员工号='" + yuangong + "'";

                SqlCommand sda = new SqlCommand(sql, conn);
                sda.ExecuteNonQuery();

                conn.Close();
            }

        }

上面是按钮删除事件

下面是右键直接删除


  //先拖进contextMenuStrip控件,然后在datagridView1的CellMouseup事件里加下代码

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && e.RowIndex > -1 && e.ColumnIndex > -1)
            {
                contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                dataGridView1.Rows[e.RowIndex].Selected = true;
            }
        }

        //右键删除事件
        private void 删除一行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确实要删除该行吗?", "询问", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                //获取点击datagridview1的行的 行号
                int r = this.dataGridView1.CurrentRow.Index;
                //获取此行的 员工编号 的值
                string yuangong = this.dataGridView1.Rows[r].Cells["员工号"].Value.ToString();
                //删除 datagridview1 的选中行
                this.dataGridView1.Rows.Remove(this.dataGridView1.Rows[r]);
                //删除数据库的 员工编号 的对应行
                string str = "server=172.24.140.16;database=UnionTrade;User ID=union_admin;Password=Tianjin@2017";  //数据库链接字符串
                SqlConnection conn = new SqlConnection(str);                            //实例化链接
                conn.Open();                        //打了链接
                string sql = "delete from shetuanxinxi where 员工号='" + yuangong + "'";

                SqlCommand sda = new SqlCommand(sql, conn);
                sda.ExecuteNonQuery();

                conn.Close();
            }
        }

本人是听力障碍者,今年毕业于天津理工大学聋人工学院,目前在一家公司做应用软件程序员。我今天写这个东西是为了帮助遇到问题无法解决的朋友们解决。

找我可以加微信826343594

第2个回答  推荐于2018-03-23
for(int i=0;i<n-1;i++)
{
if (dataGridView1.Rows[i].Selected == true)
{
string tmp=dataGridView1.Rows[i].Cells[0].Tostring();
不行试试这个string tmp=dataGridView1.Rows[i].Cells[0].Value.Tostring();
strng sql="delete from 表 where 字段=tmp";
后面的你应该就知道了吧
dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
}
}本回答被提问者和网友采纳
第3个回答  2012-04-17
int id =dataGridView1.CurrentRow.Cells[0].Value; //得到选中行的第一列的值,一般第一列都是ID。
//根据ID删除数据库表记录(delete from biao where ID=id),删除代码应该会吧。
//重新读取数据库,并且绑定到dataGridView1上。绑定更会吧,跟你前面查询出来一样的。
第4个回答  2012-04-17
你删除选定行之后,再重新绑定一下就行了!删除时,最后是唯一值的字段,否则容易勿删重要信息