this prog for database to edit and search database using sql by vb
I want the search results appear in DataGridView only

        Dim winclass As New Class1

        btnsearch.Enabled = True
        'btnsearch.Enabled = True
        btnnsearch.Enabled = True 
        If txtsearch.Text.Trim = "" Then
            MsgBox("enter value of search", MsgBoxStyle.Information)
            txtsearch.Focus()
            ' Lablerror.Text = "enter value of search"
            ' Lablerror.BackColor = Color.Red
            ' Lablerror.ForeColor = Color.White

            Exit Sub
        End If
        If Class1.sqlconn.State = ConnectionState.Open Then
            Class1.sqlconn.Close()
        End If
        sqlstatment = ""
        sql1 = ""
        Dim firstcont As Boolean = True
        If txtsearch.Text <> "" Then
            If firstcont = False Then
                sqlstatment = sqlstatment & "sader.wasf like '%" & txtsearch.Text & "%' or sader.datee like '%" & txtsearch.Text & "%' or sader.nosader like '%" & txtsearch.Text & "%' or sader.moh like '%" & txtsearch.Text & "%'"
            Else
                firstcont = False
                sqlstatment = sqlstatment & "sader.wasf like '%" & txtsearch.Text & "%' or sader.datee like '%" & txtsearch.Text & "%' or sader.nosader like '%" & txtsearch.Text & "%' or sader.moh like '%" & txtsearch.Text & "%'"
            End If
        End If
        Class1.sqlconn.Open()
        sql1 = "select * from sader where " & sqlstatment
        Dim cmd As New SqlCommand(sql1)
        cmd.Connection = Class1.sqlconn
        Dim dr As SqlDataReader = cmd.ExecuteReader
        If dr.HasRows Then
            dr.Read()
            Txtnusader.Text = dr.Item("nosader")
            TxtID.Text = dr.Item("ID")
            Txtdate.Text = dr.Item("datee")
            Txtwasf.Text = dr.Item("wasf")
            Txtmulahzat.Text = dr.Item("moh")
            ' Lablerror.Text = ""
            fillDataGrid()
            dr.Close()




        Else
            Lablerror.BackColor = Color.Red
            Lablerror.ForeColor = Color.White
            Lablerror.Text = "no data"
            'btnsearch.Enabled = False

like this ?

datagridview.rows(index).cells(index).value = dr.item("ID")

Why don't you just bind the DataGridView with the sql?

commented: I do not understand +0

I tried and did not work I hope you further explanation

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.