如何在C# WinForm中删除DataGridView选中行并同步删除Excel数据
问题解决:DataGridView行删除同步Excel记录
问题说明
在C# Windows Form中,选中DataGridView的行删除数据时,仅能删除DataGridView中的行,无法同步删除Excel表格里的对应记录。
解决方案
原代码仅对DataGridView的行进行了移除操作,没有对Excel文件执行删除逻辑。需要针对每一条选中的记录,通过OleDb执行DELETE语句删除Excel中对应的行,同时确保操作完成后重新加载DataGridView以保持数据同步。
关键步骤
- 选中行时获取唯一标识(比如第一列的车牌号
plakaNo,假设它是唯一的) - 对每一条选中记录,执行
DELETE FROM [Sayfa1$] WHERE 列名 = 对应值的SQL语句 - 使用
using语句管理数据库连接和命令,确保资源正确释放 - 删除完成后重新从Excel加载数据到DataGridView,保证界面和文件数据一致
修改后的完整代码
using System.Data.OleDb; namespace Ünistop { public partial class SoforİlanlarıGoruntuleForm : Form { public SoforİlanlarıGoruntuleForm() { InitializeComponent(); this.StartPosition = FormStartPosition.CenterScreen; } public static DataTable ExcelTabloDondur(string yol) { DataTable tablo = new DataTable(); string sorgu = "select*from[Sayfa1$]"; // 使用using自动释放连接资源 using (OleDbConnection baglanti = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + yol + ";Extended Properties=Excel 8.0;")) { try { baglanti.Open(); using (OleDbCommand cekilenveri = new OleDbCommand(sorgu, baglanti)) { cekilenveri.CommandTimeout = 250; using (OleDbDataReader oku = cekilenveri.ExecuteReader(CommandBehavior.Default)) { tablo.Load(oku); } } } catch { // 可添加异常日志逻辑 } } return tablo; } private void SoforİlanlarıGoruntuleForm_Load(object sender, EventArgs e) { YukleExcelVerileri(); } // 封装加载数据方法,方便重复调用 private void YukleExcelVerileri() { DataTable tablo = ExcelTabloDondur("ilanlar.xls"); dataGridView1.DataSource = tablo; } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0) return; // 避免点击表头出错 int secilen = e.RowIndex; plakaNo_txt.Text = dataGridView1.Rows[secilen].Cells[0].Value?.ToString() ?? ""; aracKapasitesi_txt.Text = dataGridView1.Rows[secilen].Cells[1].Value?.ToString() ?? ""; sehir_txt.Text = dataGridView1.Rows[secilen].Cells[2].Value?.ToString() ?? ""; durak_txt.Text = dataGridView1.Rows[secilen].Cells[3].Value?.ToString() ?? ""; } private void sil_btn_Click(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count == 0) { MessageBox.Show("Lütfen silmek için bir ilan seçin.", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (DialogResult.Yes != MessageBox.Show("İlanınızı silmek istediğinize emin misiniz?", "İlan Silme", MessageBoxButtons.YesNo)) { return; } string yol = "ilanlar.xls"; // F1对应Excel第一列,若表格有列名,替换为实际列名(如PlakaNo),同时修改连接字符串添加HDR=YES string deleteSorgu = "DELETE FROM [Sayfa1$] WHERE F1 = @PlakaNo"; using (OleDbConnection baglanti = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source =" + yol + ";Extended Properties=Excel 8.0;")) { baglanti.Open(); foreach (DataGridViewRow secilen in this.dataGridView1.SelectedRows) { string plakaNo = secilen.Cells[0].Value?.ToString(); if (string.IsNullOrEmpty(plakaNo)) continue; using (OleDbCommand silKomutu = new OleDbCommand(deleteSorgu, baglanti)) { // 添加参数避免SQL注入 silKomutu.Parameters.AddWithValue("@PlakaNo", plakaNo); silKomutu.ExecuteNonQuery(); } } } // 重新加载数据,同步界面与Excel文件 YukleExcelVerileri(); MessageBox.Show("Seçilen ilanlar silinmiştir.", "İlan Silme", MessageBoxButtons.OK, MessageBoxIcon.Warning); plakaNo_txt.Clear(); aracKapasitesi_txt.Clear(); sehir_txt.Clear(); durak_txt.Clear(); } } }
注意事项
- 若Excel表格第一行是列名,需修改连接字符串的
Extended Properties为Excel 8.0;HDR=YES;,同时将DELETE语句中的F1替换为实际列名 - 确保
ilanlar.xls文件路径正确,若文件不在程序运行目录下,需使用完整绝对路径 - 若使用Excel 2007及以上版本(.xlsx格式),需更换OleDb驱动为
Microsoft.ACE.OLEDB.12.0,连接字符串调整为Provider=Microsoft.ACE.OLEDB.12.0;Data Source=yol;Extended Properties="Excel 12.0 Xml;HDR=YES;"
内容的提问来源于stack exchange,提问作者nothing
相关产品推荐
相关产品推荐

