如何从datagridview和数据库中删除选定的行
这个想法是,删除时选择的行将从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于某种原因,即使我从有效的添加按钮复制了刷新代码,datagridview 也不会更新。但它确实从数据库中删除了该记录。
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.
如果只允许在 DataGridView 中进行一项选择,您可以这样做。
假设 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.