Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I populate records in datagridview1? 1

Status
Not open for further replies.

Oliver76

Programmer
Oct 2, 2001
60
0
0
I am using Mysql 5.0, and i'm having a hard time populating my records in my grid, this is supposed to be a search wherein i'm going to display the filtered records in my DataGridView1 object.

here is my unfinished code:

Dim connString As String = "Database=mydb;Data Source=localhost;User Id=root;"

Dim conn As New MySqlConnection()

Try
conn.ConnectionString = "Database=mydb;Data Source=localhost;User Id=root;"
conn.Open()
Console.WriteLine("Connection Opened")


Dim Srch As String = "SELECT * FROM tbl WHERE Fname LIKE '" & "%" & TextBox1.Text & "'"

DataGridView1.DataSource = Srch
DataGridView1.Refresh()


Catch ex As MySqlException
Console.WriteLine("Error: " & ex.ToString())
Finally
conn.Close()
Console.WriteLine("Connection Closed")

End Try
End Sub
 

You're halfway there, you just need to actually get the data and display it.

Code:
        Dim connString As String = "Database=mydb;Data Source=localhost;User Id=root;"

        Dim conn As New MySqlConnection()

        Try
            conn.ConnectionString = "Database=mydb;Data Source=localhost;User Id=root;"
            conn.Open()
            Console.WriteLine("Connection Opened")


            Dim Srch As String = "SELECT * FROM tbl WHERE Fname LIKE '" & "%" & TextBox1.Text & "'"

            [red]Dim da As MySqlDataAdapter
            Dim dt As DataTable

            da = New MySqlDataAdapter(Srch, conn)
            dt = New DataTable

            da.Fill(dt)[/red]

            DataGridView1.DataSource = [red]dt[/red]
            DataGridView1.Refresh()


        Catch ex As MySqlException
            Console.WriteLine("Error: " & ex.ToString())
        Finally
            conn.Close()
            Console.WriteLine("Connection Closed")

        End Try

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
hello again, it work at last.... but my "%" doesnt work huhuhu, for example, I want to display all my records beginning in letter M, like "mark, moore, matthew..." when i type "M" in my textbox1.text, it doesnt display the names beginning in "M"....

i tried this.... but gives me same output... I have to put the exact record to make this one workout... example "Mark", it will display the record "Mark"

Dim Srch As String = "SELECT * FROM tbl WHERE Fname LIKE '%" & TextBox1.Text & "'"

tnx in advance...
 
hahahaha I Got IT!!!!!!!!! fool am i.... I just need to put the "%" after my textbox1.text.... sorry.... :(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top