Hi All,

I want to know how to bind data to DataGridView Control.
I have seen examples on google nd other sites. but all examples use DataBind() while in my program, it shows DataBindings() in intellisense. I cant find out how to do this. I have to bind data from Sql Database Table.

Hope my problem is clear.

Thanks.

Recommended Answers

All 8 Replies

There are many ways to bind data to the DataGridView. The method i use is
1. Create columns in the DataGridView control.
2. Create DataTable with the same columns.
3. Create Rows for the Datatable and insert data in to the row.
4. Set DataSource property of the DataGridview control to the created table.
5. Call DataBind method.

I think DataBinding is a property not a method.

commented: Very good +5

tht's wht my problem is.. When I call DataBind Method it shows error stating : 'DataBind' is not a member of 'System.Windows.Forms.DataGridView'.
This I am not able to resolve.

Dim con As SqlConnection
        Dim sql As String = ""
        Dim cmd As SqlCommand
        con = ("server=....")
        ...
        sql = "SELECT * FROM MYTABLE"

        con.Open()
        cmd = New SqlCommand(sql, con)
        Try
            cmd.ExecuteNonQuery()
            gridview.DataSource = cmd.ExecuteReader
            gridview.DataBind() ' Here it shows the Error

            con.Close()
            con.Dispose()

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

So, do not use DataBind() method. Please follows all steps of thread #2 - lighthead except 5th.

DataBind() is method of ASP.NET server control.

Hello sir,
What Shall I use instead of DataBind() then.
I did what lighthead suggested. But I am still not able to show the data from database to the datagridview. It does not show any errors now, but am not able to get the data also.

Thanks.

....
  Dim adp as new SqlDataAdpater("select * from table",cn)
  Dim dt as new DataTable
  adp.Fill(dt)

  datagridView1.Datasource=dt
  ...

its a web application or forms..
if it is forms, it is enough if u give DataSource.

Thanks.
Now, I am able to do it.

Thanks

Thanks,

Mark this thread as solved if you get solution.

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.