In this article I will explain with an example, how to delete DataGridView selected rows if empty in Windows Forms (WinForms) Application with C# and VB.Net.
Form Design
The Form consists of following controls:
DataGridView – For displaying data.
Button – For deleting the row.
The Button has been assigned with a Click event handler.
Namespaces
You will need to import the following namespace.
C#
VB.Net
Binding DataGridView with C# and VB.Net
Inside the Form Load event handler, the AllowUserToAddRows to property of DataGridView is set to FALSE and Columns (If exists) are cleared using Clear method.
Then, the DataGridView columns are defined using DataGridViewColumn class object.
Once the columns are defined, a DataTable class object is created and dynamic records are added to it.
Finally, DataTable is assigned to the DataSource property of DataGridView.
C#
private void Form1_Load(object sender, EventArgs e)
{
//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.
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("CustomerId"),
new DataColumn("Name"),
new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(null, null, null);
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(null, null, null);
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(null, null, null);
dt.Rows.Add(4, "Robert Schidner", "Russia");
dt.Rows.Add(null, null, null);
dataGridView1.DataSource = dt;
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'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.
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("CustomerId"), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(Nothing, Nothing, Nothing)
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(Nothing, Nothing, Nothing)
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(Nothing, Nothing, Nothing)
dt.Rows.Add(4, "Robert Schidner", "Russia")
dt.Rows.Add(Nothing, Nothing, Nothing)
dataGridView1.DataSource = dt
End Sub
Deleting DataGridView Selected Rows if Empty with C# and VB.Net
When the Delete Button is clicked, a FOR EACH loop is executed over DataGridView rows and a check is performed whether the row contains data or the row is empty using CheckIfNullOrEmpty method (explained later).
If found empty, then the row is removed using RemoveAt method.
CheckIfNullOrEmpty
Inside the CheckIfNullOrEmpty method, a FOR loop is executed over DataGridView cells and a check is performed if the cell value is NULL or empty, it returns TRUE otherwise FALSE.
C#
private void OnDelete(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
if (this.CheckIfNullOrEmpty(row))
{
dataGridView1.Rows.RemoveAt(row.Index);
}
}
}
private bool CheckIfNullOrEmpty(DataGridViewRow row)
{
for (int i = 0; i <= row.Cells.Count - 1; i++)
{
if (Convert.IsDBNull(row.Cells[i].Value)
|| string.IsNullOrEmpty(row.Cells[i].Value.ToString()))
{
return true;
}
}
return false;
}
VB.Net
Private Sub OnDelete(sender As Object, e As EventArgs) Handles btnDelete.Click
For Each row As DataGridViewRow In dataGridView1.SelectedRows
If Me.CheckIfNullOrEmpty(row) Then
dataGridView1.Rows.RemoveAt(row.Index)
End If
Next
End Sub
Private Function CheckIfNullOrEmpty(ByVal row As DataGridViewRow) As Boolean
For i As Integer = 0 To row.Cells.Count - 1
If Convert.IsDBNull(row.Cells(i).Value) OrElse String.IsNullOrEmpty(row.Cells(i).Value.ToString()) Then
Return True
End If
Next
Return False
End Function
Screenshot
Downloads