如何从datagridview和数据库中删除选定的行

发布于 2024-10-30 04:54:06 字数 557 浏览 1 评论 0原文

这个想法是,删除时选择的行将从 datagridview、数据库中删除,然后 datagridview 被刷新。我认为它必须使用 SQL 来完成,但是如何将文本类型的 sql 命令与特定行的删除代码链接起来? 该数据库由一个表组成,并且数据网格与其绑定。

删除按钮:

private void btnBookRecord_Click(object sender, EventArgs e)
{
    if (this.BooksGrid.SelectedRows.Count > 0)
    {
        foreach (DataGridViewRow dgvrCurrent in BooksGrid.SelectedRows)
        {
            if (dgvrCurrent == BooksGrid.CurrentRow)
            {
                BooksGrid.CurrentCell = null;
            }

            // Delete row code here
        }
    }
}

The idea is that the row that is selected when deleted gets removed from datagridview, database and then datagridview gets refreshed. I assume it has to be done with SQL but how would you link that sqlcommand of type text with a delete code with that particular row?
The database consists of one single table and the datagrid is bound to it.

Delete button:

private void btnBookRecord_Click(object sender, EventArgs e)
{
    if (this.BooksGrid.SelectedRows.Count > 0)
    {
        foreach (DataGridViewRow dgvrCurrent in BooksGrid.SelectedRows)
        {
            if (dgvrCurrent == BooksGrid.CurrentRow)
            {
                BooksGrid.CurrentCell = null;
            }

            // Delete row code here
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小嗲 2024-11-06 04:54:06

由于某种原因,即使我从有效的添加按钮复制了刷新代码,datagridview 也不会更新。但它确实从数据库中删除了该记录。

private void deleteRecord()
{
    if (BooksGrid.SelectedRows.Count > 0)
    {
        int selectedIndex = BooksGrid.SelectedRows[0].Index;

        int rowID = int.Parse(BooksGrid[0, selectedIndex].Value.ToString());
        string sql = "DELETE FROM Table1 WHERE RowID = @RowID";

        SqlCommand deleteRecord = new SqlCommand();
        deleteRecord.Connection = Booksconnection;
        deleteRecord.CommandType = CommandType.Text;
        deleteRecord.CommandText = sql;

        SqlParameter RowParameter = new SqlParameter();
        RowParameter.ParameterName = "@RowID";
        RowParameter.SqlDbType = SqlDbType.Int;
        RowParameter.IsNullable = false;
        RowParameter.Value = rowID;

        deleteRecord.Parameters.Add(RowParameter);

        deleteRecord.Connection.Open();

        deleteRecord.ExecuteNonQuery();

        deleteRecord.Connection.Close();

        booksDataset1.GetChanges();

        sqlDataAdapter1.Fill(booksDataset1.Videos);
    }
}

For some reason the datagridview won't update, even though I copied the refresh code from add button which works. But it does delete the record from database.

private void deleteRecord()
{
    if (BooksGrid.SelectedRows.Count > 0)
    {
        int selectedIndex = BooksGrid.SelectedRows[0].Index;

        int rowID = int.Parse(BooksGrid[0, selectedIndex].Value.ToString());
        string sql = "DELETE FROM Table1 WHERE RowID = @RowID";

        SqlCommand deleteRecord = new SqlCommand();
        deleteRecord.Connection = Booksconnection;
        deleteRecord.CommandType = CommandType.Text;
        deleteRecord.CommandText = sql;

        SqlParameter RowParameter = new SqlParameter();
        RowParameter.ParameterName = "@RowID";
        RowParameter.SqlDbType = SqlDbType.Int;
        RowParameter.IsNullable = false;
        RowParameter.Value = rowID;

        deleteRecord.Parameters.Add(RowParameter);

        deleteRecord.Connection.Open();

        deleteRecord.ExecuteNonQuery();

        deleteRecord.Connection.Close();

        booksDataset1.GetChanges();

        sqlDataAdapter1.Fill(booksDataset1.Videos);
    }
}
紅太極 2024-11-06 04:54:06

如果只允许在 DataGridView 中进行一项选择,您可以这样做。

假设 DataGridView 中的第一列是数据库中行的标识种子。

if (BooksGrid.SelectedRows.Count > 0)
{
     int selectedIndex = BooksGrid.SelectedRows[0].Index;

     // gets the RowID from the first column in the grid
     int rowID = int.Parse(BooksGrid[0, selectedIndex].Value.ToString());

     string sql = "DELETE FROM Table1 WHERE RowID = @RowID";

     // your code for deleting it from the database

     // then your code for refreshing the DataGridView
}

If only allowing one selection in the DataGridView, you could do this.

Say the first column in the DataGridView is the identity seed of the row in the database.

if (BooksGrid.SelectedRows.Count > 0)
{
     int selectedIndex = BooksGrid.SelectedRows[0].Index;

     // gets the RowID from the first column in the grid
     int rowID = int.Parse(BooksGrid[0, selectedIndex].Value.ToString());

     string sql = "DELETE FROM Table1 WHERE RowID = @RowID";

     // your code for deleting it from the database

     // then your code for refreshing the DataGridView
}
自由范儿 2024-11-06 04:54:06
DataGridView datagridview1;

datagridview1.Rows.remove(datagridview1.SelectedRows[0]);
DataGridView datagridview1;

datagridview1.Rows.remove(datagridview1.SelectedRows[0]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文