本文介绍了保存到数据库DataGridView中的改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 DataGridView的
和删除按钮
一种形式,我对这个代码删除一行,如何使该删除保存到数据库?我充满了 dataGridView1
与的DataSet
。
如果(dataGridView1.Rows.Count大于0)
{
如果(dataGridView1.Rows [dataGridView1.CurrentRow.Index] .IsNewRow =真!)
{
dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
}
}
解决方案
假设在你的表
您有一个名为列 ID
并希望根据从数据库中删除ID
。试试这个:
私人无效btnDelete_Click(对象发件人,EventArgs五)
{
的foreach(的DataGridViewRow项目在dataGridView1.SelectedRows)
{
VAR ID = item.Cells [0] .Value.ToString(); //你可以改变ID和细胞[0]为您的需要
//写,删除这样的代码
dataGridView1.Rows.RemoveAt(item.Index)(表其中id = @id删除); //从dataGridView1
}
}
删除
I have a form with a DataGridView
and a delete Button
and I have this code for delete a row, how can make this delete saved to the database? I filled the dataGridView1
with DataSet
.
if (dataGridView1.Rows.Count > 0)
{
if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].IsNewRow != true)
{
dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
}
}
解决方案
Suppose in your Table
you have a column named id
and you want to delete from database based on id
. Try this:
private void btnDelete_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in dataGridView1.SelectedRows)
{
var id = item.Cells[0].Value.ToString();//You can change id and Cells[0] as your need
//Write Delete code like this (Delete from Table where id = @id)
dataGridView1.Rows.RemoveAt(item.Index);//Remove from dataGridView1
}
}
这篇关于保存到数据库DataGridView中的改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!