如何从C#中的Datagrid和数据库中删除选定的行 [英] How To I Delete Selected Rows From Datagrid And Database In C#

查看:75
本文介绍了如何从C#中的Datagrid和数据库中删除选定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的完整代码



here is my full code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace testdb
{
    public partial class Form1 : Form
    {
        string constr = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:/Users/Xprts_3/Documents/Database1.accdb";
       

       
        public Form1()
        {
            InitializeComponent();
            Bind();
        }
        
        private void Bind()
        {

            OleDbConnection con = new OleDbConnection(constr);   
   con.Open();
            OleDbDataAdapter da = new OleDbDataAdapter("select * from tb1", con);
            DataTable dt = new DataTable();
           da.Fill(dt);
          dataGridView1.DataSource = dt;

          


       
            con.Close();
        }

        private void btnSave_Click_Click(object sender, EventArgs e)
        {

            OleDbConnection con = new OleDbConnection(constr);   
            con.Open();
            OleDbCommand cmd = new OleDbCommand("Insert Into tb1(name) Values (@name)", con);
            cmd.Parameters.AddWithValue("name", txtproject_name.Text);
           
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Inserted sucessfully");
            Bind();
            // Clear();
       


        }
        // delete  class code starts here

        private void btnDelete_Click(object sender, EventArgs e)
        {

            int i;
            i = dataGridView1.SelectedCells[0].RowIndex;

            OleDbConnection con = new OleDbConnection(constr);
            OleDbCommand delcmd = new OleDbCommand();
            if (dataGridView1.Rows.Count > 1 && i != dataGridView1.Rows.Count - 1)
            {
                delcmd.CommandText = "DELETE FROM tb1 WHERE ID=" + dataGridView1.SelectedRows[i].Cells[0].Value.ToString() + "";
                con.Open();
                delcmd.Connection = con;
                delcmd.ExecuteNonQuery();
                con.Close();
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[i].Index);
                MessageBox.Show("Row Deleted");
            }

        }
    
    }
       
   
}







问题是,当我点击名为btnDelete的删除按钮时,它会给出一个错误,即索引超出范围。必须是非负的并且小于集合的大小。

参数名称:索引... plz告诉我解决方案




the problem is that when i am clicking on delete button named btnDelete then it is giving an error that is "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"...plz tell me the solution

推荐答案

我相信line i = dataGridView1.SelectedCells [0] .RowIndex;抛出异常。

而不是尝试使用

I believe the line i = dataGridView1.SelectedCells[0].RowIndex; is throwing the exception.
instead try using
int i = datagridView1.CurrentCell.RowIndex;





或者你可以使用

dataGridView1.SelectedRows [0] .Cells [0 ]而不是dataGridView1.SelectedRows [i] .Cells [0]



or you can use
dataGridView1.SelectedRows[0].Cells[0] instead of dataGridView1.SelectedRows[i].Cells[0]


这篇关于如何从C#中的Datagrid和数据库中删除选定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆