Hey Guys,

Im a young programmer from Dominican Republic and im wondering how do I add data from my database to my datagridview.I know how to add it when the datagrid is empty but I want to add the data to two existing columns in my grid.When I add it the way I know it just adds two more columns with the data but I want it to be displayed in my two existing columns.Can someone help!!

Edited by acepeda: n/a

5 Years
Discussion Span
Last Post by Mitja Bonca

Best and simpliest approach is to fill the DataTable from the dataBase data, and use dataSource property of dataGridView to bind data to it.


Dim table As New DataTable()
Using sqlConn As New SqlConnection("connString")
	Using da As New SqlDataAdapter("SELECT Column1, Column2 FROM MyTable", sqlConn)
		'you can add a WHERE clause in the query!
	End Using
End Using
'set data source property:
datagridview1.DataSource = New BindingSource(table, Nothing)

Tried it and it still adds two columns in the datagrid.forgot to tell you its on a Access Database.


It doesnt matter much if you use Access database.
Then before binding data with the DGV, set datasoruce to null, like:

'set data source property:
datagridview1.DataSource =  Nothing 'reset binding
datagridview1.DataSource = New BindingSource(table, Nothing)'add new data source

Now there shouldnt be adding columns. Always should be there only 2 of them.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.