Hi kana250688,
Refer below example.
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
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(0, "", "");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(0, "", "");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(0, "", "");
dt.Rows.Add(4, "Robert Schidner", "Russia");
dt.Rows.Add(0, "", "");
dataGridView1.DataSource = dt;
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
if (this.CheckIfEmpty(row))
{
dataGridView1.Rows.RemoveAt(row.Index);
}
}
}
else
{
MessageBox.Show("Please select a row.");
}
}
private bool CheckIfEmpty(DataGridViewRow row)
{
for (int i = 0; i <= row.Cells.Count - 1; i++)
{
if (row.Cells[i].Value == null || row.Cells[i].Value == DBNull.Value
|| string.IsNullOrEmpty(row.Cells[i].Value.ToString())
|| row.Cells[i].Value.ToString() == "0")
{
return true;
}
}
return false;
}
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(0, "", "")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(0, "", "")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(0, "", "")
dt.Rows.Add(4, "Robert Schidner", "Russia")
dt.Rows.Add(0, "", "")
dataGridView1.DataSource = dt
End Sub
Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
If dataGridView1.SelectedRows.Count > 0 Then
For Each row As DataGridViewRow In dataGridView1.SelectedRows
If Me.CheckIfEmpty(row) Then
dataGridView1.Rows.RemoveAt(row.Index)
End If
Next
Else
MessageBox.Show("Please select a row.")
End If
End Sub
Private Function CheckIfEmpty(ByVal row As DataGridViewRow) As Boolean
For i As Integer = 0 To row.Cells.Count - 1
If row.Cells(i).Value Is Nothing OrElse IsDBNull(row.Cells(i).Value) _
OrElse String.IsNullOrEmpty(row.Cells(i).Value.ToString()) _
OrElse row.Cells(i).Value.ToString() = "0" Then
Return True
End If
Next
Return False
End Function
Screenshot