[英]hide datagridview if there is no data in rows in vb.net
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 . 您好,我创建了一个搜索框,如果在文本框中键入了任何内容,则将在datagrid视图中获取数据,但是我的问题是,当某些键入的内容不在数据库中时,datagridview也可见。 i want ot hide the gridview if the data is not available in database .
如果数据在数据库中不可用,我想隐藏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
this above function is called in txtpname_TextChanged 上面的函数在txtpname_TextChanged中调用
check if datagridview has rows count<0 and if not hide it. 检查datagridview的行数是否小于0,如果没有,则将其隐藏。 add this after specifying datasource.
在指定数据源之后添加它。
if dgvitmsearch.rows.count=0 then
dgvitmsearch.visible=false
end if
this will do work 这会工作
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 最好检查您的
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
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.