我想在C# windows窗体中通过选择dataGridView中的一行来删除一行,但是我想从Excel表格中删除这条记录,在我写的代码中,只删除了dataGridView中选择的行,请帮助我解决这个问题。
enter image description here
enter image description here
我用下面的代码删除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$]";
OleDbConnection baglanti = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + yol + ";Extended Properties=Excel 8.0;");
try
{
baglanti.Open();
OleDbCommand cekilenveri = new OleDbCommand(sorgu, baglanti);
cekilenveri.CommandTimeout = 250;
OleDbDataReader oku = cekilenveri.ExecuteReader(CommandBehavior.Default);
tablo.Load(oku);
oku.Close();
baglanti.Close();
cekilenveri.Dispose();
oku.Dispose();
baglanti.Dispose();
}
catch
{
baglanti.Close();
baglanti.Dispose();
}
return tablo;
}
private void SoforİlanlarıGoruntuleForm_Load(object sender, EventArgs e)
{
DataTable tablo = İlanAraFormu.ExcelTabloDondur("ilanlar.xls");
dataGridView1.DataSource = tablo;
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int secilen = dataGridView1.SelectedCells[0].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)
{
string yol = "ilanlar.xls";
OleDbConnection baglanti = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source =" + yol + ";Extended Properties=Excel 8.0;");
baglanti.Open();
if (DialogResult.Yes == MessageBox.Show("İlanınızı silmek istediğinize emin misiniz?", "İlan Silme", MessageBoxButtons.YesNo))
{
foreach (DataGridViewRow secilen in this.dataGridView1.SelectedRows)
{
dataGridView1.Rows.RemoveAt(secilen.Index);
MessageBox.Show("Verdiğiniz ilan silinmiştir.", "İlan Silme", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
plakaNo_txt.Clear();
aracKapasitesi_txt.Clear();
sehir_txt.Clear();
durak_txt.Clear();
}
}
}
}
1条答案
按热度按时间px9o7tmv1#