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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically Populate a DataGridView 1

Status
Not open for further replies.

LonnieJohnson

Programmer
Apr 16, 2001
2,628
US
I am using VS 2005 and on a Windows Form I want to put a data grid. I will not know the columns of the grid until runtime. I have successfully created a means to get the fields and even create my columns on the fly. They are appearing on my grid just fine.

How do I poplate the grid itself with rows if I do not know what columns will be until the user picks them?

Thanks in advace to the Guru that can bail me out.

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 

Where is the data that the user picks coming from?

If it is coming from a database then you could bind the grid to your dataset and you will get both Headings and data.
 
Hmmmm. Sounds like what I want.

I have a list box with possible fields. They pick the fields they want to see. I take those fields and build an SQL string. I use that SQL in a data reader that gets the info from a SQL Server database.

Can I bind the data reader directly to the grid? That would be the cat's meow!!!

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
Don't use the data reader, use a dataset and bind the grid to it.

Such as follows:


Dim MyDataSet As New Data.DataSet
DataGrid1.DataSource = Nothing
DataGrid1.Refresh()
dim strSql as string="SELECT * FROM Customers")

Using MyConnection As New SqlConnection(Connstring)
myConnection.open
Using myDataAdapter As New SqlDataAdapter(strsql, MyConnection) '

myDataAdapter.Fill(MyDataSet, "Customers")
DataGrid1.DataSource = MyDataSet.Tables("Customers")

End Using
myConnection.close
End Using
 
They have got to change this so we can give more than one star. I knew there was a way but didn't know how.

Muchos gracias!!!

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top