In this article I will explain how to remove (delete) Last Blank (Empty) Row from DataGridView in Windows Forms (WinForms) Application using C# and VB.Net.
This article will explain two ways to remove (delete) Last Blank (Empty) Row from DataGridView.
1. Using Properties Window.
2. Using Code.
 
 
Form Controls
The below Form consists of a DataGridView control.
Remove (Delete) Last Blank (Empty) Row from DataGridView in Windows Forms (WinForms) Application using C# and VB.Net
 
 
Populating DataGridView
In order to populate the DataGridView, I have created a dynamic DataTable with some sample data.
C#
private void Form1_Load(object sender, EventArgs e)
{
    this.BindDataGridView();
}
 
private void BindDataGridView()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                                new DataColumn("Name", typeof(string)),
                                new DataColumn("Country",typeof(string)) });
    dt.Rows.Add(1, "John Hammond", "United States");
    dt.Rows.Add(2, "Mudassar Khan", "India");
    dt.Rows.Add(3, "Suzanne Mathews", "France");
    dt.Rows.Add(4, "Robert Schidner", "Russia");
    this.dataGridView1.DataSource = dt;
}
 
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    BindDataGridView()
End Sub
 
Private Sub BindDataGridView()
    Dim dt As New DataTable()
    dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
    dt.Rows.Add(1, "John Hammond", "United States")
    dt.Rows.Add(2, "Mudassar Khan", "India")
    dt.Rows.Add(3, "Suzanne Mathews", "France")
    dt.Rows.Add(4, "Robert Schidner", "Russia")
    Me.dataGridView1.DataSource = dt
End Sub
 
The following picture displays the DataGridView populated with records which also has a Blank (Empty) row as last row.
Remove (Delete) Last Blank (Empty) Row from DataGridView in Windows Forms (WinForms) Application using C# and VB.Net
 
 
Remove (Delete) Last Blank (Empty) Row from DataGridView
There are two ways to remove the Remove (Delete) Last Blank (Empty) Row from DataGridView.
1. Using Properties Window
The first way is to right click the DataGridView and then click Properties item from the Context menu. Now from the Properties window, look for AllowUserToAddRows property and set it False.
Remove (Delete) Last Blank (Empty) Row from DataGridView in Windows Forms (WinForms) Application using C# and VB.Net
 
2. Using Code
The second method is to set the AllowUserToAddRows property to False inside code as shown below.
C#
private void Form1_Load(object sender, EventArgs e)
{
    this.dataGridView1.AllowUserToAddRows = false;
    this.BindDataGridView();
}
 
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.dataGridView1.AllowUserToAddRows = False
    Me.BindDataGridView()
End Sub
 
Remove (Delete) Last Blank (Empty) Row from DataGridView in Windows Forms (WinForms) Application using C# and VB.Net
 
 
Downloads