简体   繁体   English

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

[英]How to delete a selected row from datagridview and database

The idea is that the row that is selected when deleted gets removed from datagridview, database and then datagridview gets refreshed. 这个想法是删除时选择的行将从datagridview,数据库中删除,然后datagridview将被刷新。 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? 我假设它必须使用SQL完成,但是如何将类型为text的sqlcommand与该特定行的删除代码相关联? 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
        }
    }
}

For some reason the datagridview won't update, even though I copied the refresh code from add button which works. 由于某种原因,即使我从添加按钮复制刷新代码,datagridview也不会更新。 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);
    }
}

If only allowing one selection in the DataGridView, you could do this. 如果只允许DataGridView中的一个选择,则可以执行此操作。

Say the first column in the DataGridView is the identity seed of the row in the database. 假设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
}
DataGridView datagridview1;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM