How To Refresh Datagridview In C# Windows Application
In this article I will explain with an example, how to refresh DataGridView subsequently tape delete in Windows (WinForms) Application using C# and VB.Net.
When the Delete Button is clicked, a Confirmation MessageBox will exist 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 information from the Database in Windows (WinForms) Application using C# and VB.Cyberspace.
Database
I accept made use of the post-obit table Customers with the schema as follows.
I have already inserted few records in the tabular array.
Notation : You can download the database tabular array SQL by clicking the download link below.
Course Pattern
Y'all volition need to add a DataGridView control. The DataGridView has been assigned CellContentClick event handler.
Namespaces
Y'all will need to import the post-obit namespaces.
C#
using Organisation.Data;
using System.Information.SqlClient;
VB.Net
Imports System.Information
Imports System.Data.SqlClient
Populating the DataGridView from Database
Inside the Grade Load issue, the DataGridView is populated with records from the Customers Table.
Kickoff the columns for displaying the data are added and and then the column for the Delete Button is added to the DataGridView.
Note : The position of the Delete Push 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 = truthful";
private void Form1_Load(object sender, EventArgs e)
{
this.BindGrid();
}
private void BindGrid()
{
//Hide the last bare line.
dataGridView1.AllowUserToAddRows = faux;
//Articulate 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 proper name = new DataGridViewTextBoxColumn();
name.HeaderText = "Proper name";
proper noun.Name = "Proper noun";
name.DataPropertyName = "Name";
name.Width = 100;
dataGridView1.Columns.Insert(1, proper name);
DataGridViewColumn state = new DataGridViewTextBoxColumn();
state.Name = "State";
country.HeaderText = "Country";
country.DataPropertyName = "Country";
country.Width = 100;
dataGridView1.Columns.Insert(2, land);
//Bind the DataGridView.
dataGridView1.DataSource = zero;
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 = threescore;
buttonColumn.Name = "buttonColumn";
buttonColumn.Text = "Delete";
buttonColumn.UseColumnTextForButtonValue = true;
dataGridView1.Columns.Insert(3, buttonColumn);
}
VB.Net
Private Const ConnectionString As Cord = "Data Source=.\SQL2017;Initial Catalog=AjaxSamples;Integrated Security = truthful"
Private Sub Form1_Load(sender As System.Object, e Equally System.EventArgs) Handles MyBase.Load
Me.BindGrid()
End Sub
Private Sub BindGrid()
'Hide the concluding 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()
proper name.HeaderText = "Name"
proper noun.Name = "Proper name"
name.DataPropertyName = "Proper name"
name.Width = 100
dataGridView1.Columns.Insert(1, name)
Dim country As DataGridViewColumn = New DataGridViewTextBoxColumn()
state.Name = "State"
country.HeaderText = "Country"
country.DataPropertyName = "Land"
country.Width = 100
dataGridView1.Columns.Insert(two, country)
'Demark the DataGridView.
dataGridView1.DataSource = Nothing
Using con As SqlConnection = New SqlConnection(ConnectionString)
Using cmd Every bit SqlCommand = New SqlCommand("SELECT CustomerId, Proper name, Land 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 Push Column.
Dim buttonColumn As DataGridViewButtonColumn = New DataGridViewButtonColumn()
buttonColumn.HeaderText = ""
buttonColumn.Width = 60
buttonColumn.Name = "buttonColumn"
buttonColumn.Text = "Delete"
buttonColumn.UseColumnTextForButtonValue = True
dataGridView1.Columns.Insert(iii, buttonColumn)
End Sub
Refreshing DataGridView afterwards record delete
When the Delete Push is clicked, the DataGridView CellContentClick effect handler is executed.
If the ColumnIndex is iii i.e. the Delete Button is clicked, then a Confirmation MessageBox us evidence and if the User clicks Yes button the Row will exist deleted (removed) from DataGridView and Database Table.
C#
individual void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (eastward.ColumnIndex == iii)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
if (MessageBox.Evidence(string.Format("Practise you desire 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.Cyberspace
Individual Sub dataGridView1_CellContentClick(sender As Organization.Object, due east As System.Windows.Forms.DataGridViewCellEventArgs) Handles dataGridView1.CellContentClick
If due east.ColumnIndex = iii Then
Dim row As DataGridViewRow = dataGridView1.Rows(e.RowIndex)
If MessageBox.Show(String.Format("Practise you lot want to delete Client ID: {0}", row.Cells("CustomerId").Value), "Confirmation", MessageBoxButtons.YesNo) = DialogResult.Yes Then
Using con Every bit 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.Shut()
End Using
End Using
Me.BindGrid()
End If
End If
End Sub
Screenshot
Downloads
Source: https://www.aspsnippets.com/Articles/Refresh-DataGridView-after-record-delete-in-Windows-Application-using-C-and-VBNet.aspx
Posted by: mcbridefarretionly.blogspot.com

0 Response to "How To Refresh Datagridview In C# Windows Application"
Post a Comment