Message box for empty DataGridView 2005

Posted on 2008-11-16
Last Modified: 2012-05-05
My DataGridView is populated when a search for customer records is done. If I can't find any customer records with a last name starting with "gra" then the DataGridView doesn't get populated with records. What I want to do is create a message if the DataGridView is Empty. What would the code be if the DataGridView is Empty?

Private Sub Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search.Click


        Dim MySQLConnection As String = My.Settings.dbConnection

        Dim dbConnection As New SqlConnection

        dbConnection.ConnectionString = MySQLConnection

        Dim bsPatients As BindingSource

        bsPatients = New BindingSource


        Dim noRecord As Integer

        noRecord = 0

        Dim strSQL As String = "SELECT FirstName, LastName, DOB, CustomerID FROM Customer WHERE LastName LIKE '" & LastName.Text & "%' ORDER BY FirstName"

        Dim myDA As SqlDataAdapter = New SqlDataAdapter(strSQL, dbConnection)

        Dim myPatient As DataTable = New DataTable


        bsPatients.DataSource = myPatient

        DataGridView1.DataSource = bsPatients


        dbConnection = Nothing

    End Sub

Open in new window

Question by:rfgraham
    LVL 15

    Accepted Solution

    Put this code after the myDA.Fill code...

            If myPatient.Rows.Count = 0 Then
                MsgBox("No serach found")
            End If
    LVL 7

    Expert Comment

    Add a bit of logic when biding the data to datagrid

     if mypatient.rows.count > 0 then
            bsPatients.DataSource = myPatient
            DataGridView1.DataSource = bsPatients
    If txtCardNumber.Text & "" Then
        Dim strScript As String = "<script language=javascript>"
        strScript += "alert('please enter Credit Card Number');"
        strScript += "</script>"
        If (Not ClientScript.IsStartupScriptRegistered("clientScript")) Then
            ClientScript.RegisterStartupScript(Me.[GetType](), "clientScript", strScript, False)
        End If
    End If

    Open in new window

    LVL 7

    Expert Comment


    Please ignore

     If txtCardNumber.Text & "" Then in the above code.Just use the javascript code to give a alert message.


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now