本文介绍了在数据库中按名称搜索记录并在gridview中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有1个表名finalsort,它有列lotno,名称,类型,大小等。



i希望按名称搜索数据并在gridview中显示它名字。



有谁能告诉我怎么做?



i不知道代码。


如果有人可以提前感谢
,那么
将被罚款。



sudeshna

I have 1 table name finalsort which has columns lotno,name,type,size etc.

i want to search data by name and display it in gridview based on name.

can anyone tell me how to do it?

i dont know the code.

will be obliged if anyone can help
thanks in advance.

sudeshna

推荐答案

dim str as String="provide name for search"
Dim cn As New SqlConnection("<your connection="" string="">")
       Dim cmd As New SqlCommand("Select * from finalsort where name='" + str + "'", cn)
       cn.Open()

       Dim da As New SqlDataAdapter(cmd)
       Dim ds As New DataSet
       da.Fill(ds)
DataGridView1.DataSource=ds.Tables(0)
DataGridView1.Refresh() 'If required
</your>


Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        'Dim strcon As String = ("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
        Dim cn As New SqlConnection("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
        'Dim da As New SqlDataAdapter
        Dim cmd As New SqlCommand
        Dim ds As New DataSet



        'Try

        cmd.Connection = cn
        cn.Open()

        Dim da As New SqlDataAdapter("select * from finalsort where name='" & ComboBox1.SelectedItem & "'", cn)


        ds = New DataSet
        da.Fill(ds)

FinalsortDataGridView.DataSource = ds.Tables(0)
FinalsortDataGridView.Refresh()
        cn.Close()
    End Sub


Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim constr As String = "<your connection="" string="">"
        Dim cn As New SqlConnection(constr)
        cn.Open()
        Dim da As New SqlDataAdapter("Select * from Table1 where name='" & ComboBox1.SelectedItem & "'", cn)
        Dim ds As New DataSet()
        da.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)
        DataGridView1.Refresh()
        cn.Close()
    End Sub</your>


这篇关于在数据库中按名称搜索记录并在gridview中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:12