Reputation: 29
Hello i have created a search box and the data is fetched in datagrid view if any thing is typed in the text box , but my problem is when some thing typed is not in the database then also the datagridview is visible . i want ot hide the gridview if the data is not available in database .
If (txtpname.Text <> "") Then
Try
con = New System.Data.OleDb.OleDbConnection(connectionString)
con.Open()
Dim ds As DataSet = New DataSet
Dim adapter As New OleDb.OleDbDataAdapter
Dim sql As String
Dim s As String
s = txtpname.Text
sql = "SELECT product_name as `Product` , rate as `Rate`,category as `Category`, product_id as `pid` FROM products where product_name like '" & (s) & "%' AND deleted='N' order by product_id ;"
adapter.SelectCommand = New OleDb.OleDbCommand(sql, con)
adapter.Fill(ds)
dgvitmsearch.DataSource = ds.Tables(0)
dgvitmsearch.Columns("Product").Width = 220
dgvitmsearch.Columns("Rate").Visible = False
dgvitmsearch.Columns("Category").Width = 148
dgvitmsearch.Columns("pid").Visible = False
con.Close()
Catch ex As Exception
MsgBox("error found")
End Try
this above function is called in txtpname_TextChanged
Upvotes: 0
Views: 2018
Reputation: 21905
It's better to check whether your dataset(ds)
's table ds.Tables
have rows or not and write an IF
condition just before filling the datagrid
like below
If ds.Tables(0).Rows.Count > 0 Then
'fill datagrid with your dataset
else
'change visibility here(false)
End If
the actual code should write like:
If ds.Tables(0).Rows.Count > 0 Then
dgvitmsearch.DataSource = ds.Tables(0)
dgvitmsearch.Columns("Product").Width = 220
dgvitmsearch.Columns("Rate").Visible = False
dgvitmsearch.Columns("Category").Width = 148
dgvitmsearch.Columns("pid").Visible = False
Else
dgvitmsearch.visible = False
End If
Upvotes: 0
Reputation: 1618
check if datagridview has rows count<0 and if not hide it. add this after specifying datasource.
if dgvitmsearch.rows.count=0 then
dgvitmsearch.visible=false
end if
this will do work
Upvotes: 1