In this article I will explain with an example, how to refresh DataGridView after record delete in Windows (WinForms) Application using C# and VB.Net.
When the Delete Button is clicked, a Confirmation MessageBox will be displayed and if the User clicks Yes button the Row will be deleted (removed) from DataGridView.
Then the DataGridView will be refreshed by again populating fresh data from the Database in Windows (WinForms) Application using C# and VB.Net.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Form Design
You will need to add a DataGridView control. The DataGridView has been assigned CellContentClick event handler.
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Populating the DataGridView from Database
Inside the Form Load event, the DataGridView is populated with records from the Customers Table.
First the columns for displaying the data are added and then the column for the Delete Button is added to the DataGridView.
Note: The position of the Delete Button changes when the DataGridView is populated with data and hence all the columns are cleared and added when the DataGridView is populated.
C#
private const string ConnectionString = @"Data Source=.\SQL2017;Initial Catalog=AjaxSamples;Integrated Security = true";
private void Form1_Load(object sender, EventArgs e)
{
this.BindGrid();
}
private void BindGrid()
{
//Hide the last blank line.
dataGridView1.AllowUserToAddRows = false;
//Clear Columns.
dataGridView1.Columns.Clear();
//Add Columns.
DataGridViewColumn customerId = new DataGridViewTextBoxColumn();
customerId.Name = "CustomerId";
customerId.HeaderText = "CustomerId Id";
customerId.DataPropertyName = "CustomerId";
customerId.Width = 100;
dataGridView1.Columns.Insert(0, customerId);
DataGridViewColumn name = new DataGridViewTextBoxColumn();
name.HeaderText = "Name";
name.Name = "Name";
name.DataPropertyName = "Name";
name.Width = 100;
dataGridView1.Columns.Insert(1, name);
DataGridViewColumn country = new DataGridViewTextBoxColumn();
country.Name = "Country";
country.HeaderText = "Country";
country.DataPropertyName = "Country";
country.Width = 100;
dataGridView1.Columns.Insert(2, country);
//Bind the DataGridView.
dataGridView1.DataSource = null;
using (SqlConnection con = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
}
//Add the Button Column.
DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
buttonColumn.HeaderText = "";
buttonColumn.Width = 60;
buttonColumn.Name = "buttonColumn";
buttonColumn.Text = "Delete";
buttonColumn.UseColumnTextForButtonValue = true;
dataGridView1.Columns.Insert(3, buttonColumn);
}
VB.Net
Private Const ConnectionString As String = "Data Source=.\SQL2017;Initial Catalog=AjaxSamples;Integrated Security = true"
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.BindGrid()
End Sub
Private Sub BindGrid()
'Hide the last blank line.
dataGridView1.AllowUserToAddRows = False
'Clear Columns.
dataGridView1.Columns.Clear()
'Add Columns.
Dim customerId As DataGridViewColumn = New DataGridViewTextBoxColumn()
customerId.Name = "CustomerId"
customerId.HeaderText = "CustomerId Id"
customerId.DataPropertyName = "CustomerId"
customerId.Width = 100
dataGridView1.Columns.Insert(0, customerId)
Dim name As DataGridViewColumn = New DataGridViewTextBoxColumn()
name.HeaderText = "Name"
name.Name = "Name"
name.DataPropertyName = "Name"
name.Width = 100
dataGridView1.Columns.Insert(1, name)
Dim country As DataGridViewColumn = New DataGridViewTextBoxColumn()
country.Name = "Country"
country.HeaderText = "Country"
country.DataPropertyName = "Country"
country.Width = 100
dataGridView1.Columns.Insert(2, country)
'Bind the DataGridView.
dataGridView1.DataSource = Nothing
Using con As SqlConnection = New SqlConnection(ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
dataGridView1.DataSource = dt
End Using
End Using
End Using
End Using
'Add the Button Column.
Dim buttonColumn As DataGridViewButtonColumn = New DataGridViewButtonColumn()
buttonColumn.HeaderText = ""
buttonColumn.Width = 60
buttonColumn.Name = "buttonColumn"
buttonColumn.Text = "Delete"
buttonColumn.UseColumnTextForButtonValue = True
dataGridView1.Columns.Insert(3, buttonColumn)
End Sub
Refreshing DataGridView after record delete
When the Delete Button is clicked, the DataGridView CellContentClick event handler is executed.
If the ColumnIndex is 3 i.e. the Delete Button is clicked, then a Confirmation MessageBox us show and if the User clicks Yes button the Row will be deleted (removed) from DataGridView and Database Table.
C#
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 3)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
if (MessageBox.Show(string.Format("Do you want to delete Customer ID: {0}?", row.Cells["CustomerId"].Value), "Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
using (SqlConnection con = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM Customers WHERE CustomerId = @CustomerId", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@CustomerId", row.Cells["CustomerId"].Value);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
this.BindGrid();
}
}
}
VB.Net
Private Sub dataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dataGridView1.CellContentClick
If e.ColumnIndex = 3 Then
Dim row As DataGridViewRow = dataGridView1.Rows(e.RowIndex)
If MessageBox.Show(String.Format("Do you want to delete Customer ID: {0}", row.Cells("CustomerId").Value), "Confirmation", MessageBoxButtons.YesNo) = DialogResult.Yes Then
Using con As New SqlConnection(ConnectionString)
Using cmd As New SqlCommand("DELETE FROM Customers WHERE CustomerId = @CustomerId", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@CustomerId", row.Cells("CustomerId").Value)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Me.BindGrid()
End If
End If
End Sub
Screenshot
Downloads