Resolved how to check if datagridview is empty

rajdh75

Active member
Joined
Mar 30, 2020
Messages
29
Programming Experience
Beginner
Hello,
I am using Vb.net.
I have a datagridview1 in winform for adding product, quantity, rate, amount, etc.
I have other textboxes for Customer Name and Address.
The data is saved with a button - SAVE.
I have a code to check if any textbox is empty as -
VB.NET:
If TxtCustomerName.Text = "" Or TxtAddress.Text = "" Then
            MsgBox("Please fill all record.")
            Exit Sub
        End If
This code works well.
I want a code if someone accidently click SAVE button before adding Product, there should be message as "Please add product."
I have tried code - DataGridView1.Rows.Count=0, DataGridView1.Rows.Count < 1, datagridview1.rowcount=0
but they do not work.
Thanks.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,538
Location
Norway
Programming Experience
10+
When user can add rows the RowCount will always be 1 or higher, where first row is the "new row". If something is entered in any cell of the "new row" then another "new row" is added. So you can check for RowCount>1.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,440
Location
Sydney, Australia
Programming Experience
10+
@JohnH's suggestion is probably all you need but, for the sake of completeness, a solution that would work whether AllowUserToAddRows is True or not is as follows:
VB.NET:
If Not myDataGridView.Rows.Cast(Of DataGridViewRow)().Any(Function(row) row.IsNewRow) Then
    'The grid contains no populated rows
End If
You could also do this:
VB.NET:
If (myDataGridView.AllowUserToAddRows AndAlso myDataGridView.RowCount = 1) OrElse
   (Not myDataGridView.AllowUserToAddRows AndAlso myDataGridView.RowCount = 0) Then
    'The grid contains no populated rows
End If
You could turn either of those into an extension method that you could call on any DataGridView:
VB.NET:
Imports System.Runtime.CompilerServices

Public Module DataGridViewExtensions

    <Extension>
    Public Function IsEmpty(source As DataGridView) As Boolean
        Return Not source.Rows.Cast(Of DataGridViewRow)().Any(Function(row) row.IsNewRow)
    End Function
    'or
    <Extension>
    Public Function IsEmpty(source As DataGridView) As Boolean
        Return (source.AllowUserToAddRows AndAlso source.RowCount = 1) OrElse
               (Not source.AllowUserToAddRows AndAlso source.RowCount = 0)
    End Function

End Module
Sample usage:
VB.NET:
If myDataGridView.IsEmpty() Then
    'The grid contains no populated rows
End If
 

rajdh75

Active member
Joined
Mar 30, 2020
Messages
29
Programming Experience
Beginner
Thanks JohnH for your code.
It is giving message after I Add a Product and not before. I wanted to give message if there is no rows in datagridview1.
So I changed code to
VB.NET:
If DataGridView1.RowCount > 1 = False Then
            MsgBox("Please Add Product.")
            Exit Sub
        End If
then it work perfectly.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,538
Location
Norway
Programming Experience
10+
There is no need to compare expression with False. Just check If expression Then. If wanting to check a false expression use the Not operator If Not expression Then, but that is not needed here. With numeric expression you can instead use <>= comparisons and check If count = 1 or If count < 2
 
Top Bottom