i am adding , deleting and updating data to database in Winforms. i have gridview on all add,delete and update forms. After deleting records on clicking "delete" button deleted record should removed instantly from dataGridView.
////Please note that DATABIND is giving error ie using or assembly reference missing.
Code Behind:
private void btnDelete_Click(object sender, EventArgs e)
{
if (txtIDD.Text == "")
{
MessageBox.Show("Please fill ID no. of record to Delete", "Important Message");
}
else
{
try
{
OleDbCommand Cmd = new OleDbCommand();
Cmd.Connection = conn;
conn.Open();
Cmd.CommandText = "DELETE FROM AddressBook WHERE ID="+txtIDD.Text;
Cmd.CommandType = CommandType.Text;
Cmd.ExecuteNonQuery();
Cmd.Connection.Close();
conn.Close();
dataGridView3.Update();
MessageBox.Show("Delete Succesfull");
}
catch (System.Exception err)
{
dataGridView3.DataSource = dt;
dataGridView3.DataBind();
dataGridView3.Update();
this.label27.Visible = true;
this.label27.Text = err.Message.ToString();
}
}
}
Bind the grid again to fetch values form database after add, update and delete etc.
private void btnDelete_Click(object sender, EventArgs e)
{
if (txtIDD.Text == "")
{
MessageBox.Show("Please fill ID no. of record to Delete", "Important Message");
}
else
{
try
{
OleDbCommand Cmd = new OleDbCommand();
Cmd.Connection = conn;
conn.Open();
Cmd.CommandText = "DELETE FROM AddressBook WHERE ID="+txtIDD.Text;
Cmd.CommandType = CommandType.Text;
Cmd.ExecuteNonQuery();
Cmd.Connection.Close();
conn.Close();
**//Call a method that binds the grid or get DataTable from database and bind it like this**
dataGridView3.DataSource = dt;
dataGridView3.Update();
MessageBox.Show("Delete Succesfull");
}
catch (System.Exception err)
{
this.label27.Visible = true;
this.label27.Text = err.Message.ToString();
}
}
}
Create a function whereby you can reload the grid
private method ReloadGrid()
{
// first load your datatable/dt then set it as the datasource of your grid
dataGridView3.DataSource = dt;
dataGridView3.DataBind();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.