[英]hide datagridview if there is no data in rows in vb.net
您好,我創建了一個搜索框,如果在文本框中鍵入了任何內容,則將在datagrid視圖中獲取數據,但是我的問題是,當某些鍵入的內容不在數據庫中時,datagridview也可見。 如果數據在數據庫中不可用,我想隱藏gridview。
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
上面的函數在txtpname_TextChanged中調用
檢查datagridview的行數是否小於0,如果沒有,則將其隱藏。 在指定數據源之后添加它。
if dgvitmsearch.rows.count=0 then
dgvitmsearch.visible=false
end if
這會工作
最好檢查您的dataset(ds)
的表ds.Tables
是否有行,並在填充datagrid
之前編寫IF
條件,如下所示
If ds.Tables(0).Rows.Count > 0 Then
'fill datagrid with your dataset
else
'change visibility here(false)
End If
實際代碼應如下所示:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.