Link to home
Start Free TrialLog in
Avatar of rfgraham
rfgrahamFlag for United States of America

asked on

Message box for empty DataGridView vb.net 2005

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
 
        'DATAGRID MULTIPLE RESULTS LIST
        Dim MySQLConnection As String = My.Settings.dbConnection
        Dim dbConnection As New SqlConnection
        dbConnection.ConnectionString = MySQLConnection
 
        Dim bsPatients As BindingSource
        bsPatients = New BindingSource
        dbConnection.Open()
 
        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
 
        myDA.Fill(myPatient)
 
 
        bsPatients.DataSource = myPatient
 
        DataGridView1.DataSource = bsPatients
 
        dbConnection.Close()
 
        dbConnection = Nothing
 
 
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of JackOfPH
JackOfPH
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lakshmidurga
lakshmidurga

Add a bit of logic when biding the data to datagrid

myDA.Fill(myPatient)
 
 if mypatient.rows.count > 0 then
        bsPatients.DataSource = myPatient
 
        DataGridView1.DataSource = bsPatients
else
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
    
   DataGridView1.visible=false
    
    
End If
 
 
        dbConnection.Close()

Open in new window

Hi,

Please ignore

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