【问题标题】:C# - How to delete selected rows from a DataGridView?C# - 如何从 DataGridView 中删除选定的行?
【发布时间】:2019-10-13 17:33:39
【问题描述】:

我正在尝试删除用户在 C# WinForms 应用程序的 DataGridView 中选择的行(连接到已经有记录的本地数据库)。

我已经实现了下面的代码,并且没有错误,但是删除从未被拾取(即使消息框显示一条记录已被“删除”,这是不正确的,因为该记录仍保留在 DataGrid 中)

另请注意,删除功能在搜索功能中 - 请参见下面的代码。

public void DisplayData()
    {
        string ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename= C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA\Library System Project.mdf ;Integrated Security=True;Connect Timeout=30";

        string Query = "select * from Customers";

        SqlConnection DBCon = new SqlConnection(ConnectionString);
        SqlCommand DBCommand = new SqlCommand(Query, DBCon);
        SqlDataReader DBReader;

        try
        {
            DBCon.Open();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(DBCommand);
            da.Fill(dt);
            dgv_CustomerDetails.DataSource = dt;
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        finally
        {
            // *** If you're going to be opening a connection be sure to close it ***
            // *** Finally blocks work well for this ***
            DBCon.Close();
        }
    }


    private void SearchCustomerRecordForm_Load(object sender, EventArgs e)
    {
        DisplayData();
    }


    private void btnDeleteCustomerRecord_Click(object sender, EventArgs e)
    {
        string ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename= C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA\Library System Project.mdf ;Integrated Security=True;Connect Timeout=30";

        int rowIndex = dgv_CustomerDetails.CurrentCell.RowIndex;

        string Query = "delete from Customers where CustomerName = '"+ dgv_CustomerDetails.CurrentCell(rowIndex) +"'";

        SqlConnection DBCon = new SqlConnection(ConnectionString);
        SqlCommand DBCommand = new SqlCommand(Query, DBCon);
        SqlDataReader DBReader;

        try
        {
            DBCon.Open();
            DBReader = DBCommand.ExecuteReader();
            MessageBox.Show("Customer record removed from the system.", "Library System", MessageBoxButtons.OK);
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        finally
        {
            // *** If you're going to be opening a connection be sure to close it ***
            // *** Finally blocks work well for this ***
            DBCon.Close();
            DisplayData();
        }
    }

【问题讨论】:

    标签: c# sql visual-studio datagrid sql-delete


    【解决方案1】:

    您确定查询创建良好吗?该行似乎缺少单元格值属性:

    string Query = "delete from Customers where CustomerName = '"+ dgv_CustomerDetails.CurrentCell(rowIndex).value +"'";
    

    尝试查看 Query 变量的值,因为它是建立起来的。

    【讨论】:

    • 您好 Jan,我已将查询更改为 'where CustomerID' 而不是 CustomerName,但出现错误:将 varchar 值转换为数据类型 int 时转换失败
    • 我也尝试在上面查询,它给了我一个 CurrentCell 错误(不可调用的成员 'DataGridView.CurrentCell' 不能像方法一样使用。)
    • 当我使用它时,我不是直接从单元格中获取值,而是通过这种方式从数据层获取值:(对不起,我是 VB.NET 人)Dim dat As DataRowView = DataGridView1.SelectedRows(0)。 DataBoundItem ID = dat.Item("CustomerID")
    • 既然你引用了数字,它不能用撇号括起来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多