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!!

Recommended Answers

All 3 Replies

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

Like:

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!
		da.Fill(table)
	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.

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.