دوست عزیز (mahdi_7610) من استفاده از کد زیر رو به شما پیشنهاد می کنم. این کد رو خودم نوشتم و امتحان کردم و کاملاً درسته و جواب میده. شاید تو بعضی از قسمت ها از روشهای غیر اصولی استفاده کردم ولی مهم اینه که کاری که میخوای رو انجام میده :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace DataGridViewSample
{
public partial class Form1 : Form
{
private string[] dataGridColumnsHeadersText = { "کد کتاب", "نام کتاب", "ناشر", "توضیحات" };
private string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + new System.IO.FileInfo(Application.ExecutablePath).Dir ectoryName + "\\SampleDataBase.mdb";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadTBookTable();
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (txtID.Text.Length > 0)
{
if (ExistRecord(txtID.Text))
{
if (MessageBox.Show("آیا مطمئنید که می خواهید رکورد مورد نظر را حذف کنید؟", "Question", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
{
DeleteRecordByID(txtID.Text);
// baraye hazfe satre dataGridView az code zir estefade kon...
for (int counter = 0; counter < dataGridView1.Rows.Count - 1; counter++)
if (dataGridView1.Rows[counter].Cells[0].Value.ToString() == txtID.Text)
dataGridView1.Rows.RemoveAt(counter);
// -----------------------------------------------------------
MessageBox.Show("رکورد مورد نظر حذف شد", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("رکورد مورد نظر موجود نیست", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtID.Focus();
txtID.SelectAll();
}
}
else
{
MessageBox.Show("لطفاً فیلد مورد نظر را پر کنید", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtID.Focus();
}
}
private void LoadTBookTable()
{
DataSet table;
OleDbDataAdapter dataAdapter;
try
{
table = new DataSet();
dataAdapter = new OleDbDataAdapter("SELECT * FROM TBook", connectionString);
dataAdapter.Fill(table, "TBook");
dataGridView1.DataSource = table;
dataGridView1.DataMember = "TBook";
for (int counter = 0; counter < dataGridColumnsHeadersText.Length; counter++)
dataGridView1.Columns[counter].HeaderText = dataGridColumnsHeadersText[counter];
dataGridView1.Columns[1].Width = 212;
}
catch (OleDbException exp)
{
MessageBox.Show(exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
table = null;
dataAdapter = null;
}
}
private bool ExistRecord(string strID)
{
bool result = false;
// -------------------------------------------
DataSet table;
OleDbDataAdapter dataAdapter;
try
{
table = new DataSet();
dataAdapter = new OleDbDataAdapter("SELECT * FROM TBook WHERE ID='" + strID + "'", connectionString);
dataAdapter.Fill(table, "TBook");
if (table.Tables["TBook"].Rows.Count > 0)
result = true;
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
table = null;
dataAdapter = null;
}
// -------------------------------------------
return result;
}
private bool DeleteRecordByID(string strID)
{
bool result = false;
// -------------------------------------------
OleDbConnection connection;
OleDbCommand command;
try
{
connection = new OleDbConnection(connectionString);
command = new OleDbCommand("DELETE * FROM TBook WHERE ID='" + strID + "'", connection);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
result = true;
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
command = null;
connection = null;
}
// -------------------------------------------
return result;
}
}
}