How to dynamically chage a gridview to display and update any given table in a database.

I am using Visual Studio 2005/ ASP.NET 2.0, and I am using a drop-down list to display all the user tables in my database. What I want to do is use this drop-down to select a table and have this table show up in a Gridview for display/editing/insertion purposes. When the table is viewed/updated/inserted, I want the user to be able to select another table from the drop-down and display/edit/insertion for the NEW table. How can I accomplish this? Can this be done with one form by dynamically creating/updating the Gridview? I already have a form with the drop-down working and displaying one table via a Gridview. My Gridview is able to edit/update/cancel, but no insertion. I couldn't get the Gridview to insert a new record to the table, so I added some textboxes and a button to do the Insertion with a stored procedure. As I stated previously, I need this functionality for every table in my database. Any help would be MOST appreciated. (I am running out of time here, so I need some help ASAP.)


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Assuming you are using SQL Server, you can use the following PAQ to see how to list all your tables:

After that, you should be able to just alter your SQL statement for INSERTS, etc., to reflect the table you are in.  

Is this a good start for you, or am I misunderstanding what you are wanting to do?
Just set AutoGeneratecolumns to true on the gridview (in the on load or something) and have all the tables in one dataset.

Then all you have to do is put on the pulldown SelectedIndexChanged (or SelectedValue?)
DataGridView1.DataSource = DataSet.TableName

I think in the tableadapter you can set it up to use your stored procedures and what not?
DiskScannerAuthor Commented:
Thanks guys,
divinewind80, I am using SQL Server 2000 and I am already using something like the PAQ to populate my drop-down lilst.

SELECT name AS TableName FROM sysobjects WHERE (xtype = 'u') AND (NOT (name LIKE N'%dtProp%'))

Then after the user selects a table from the drop-down and clikcking the 'Display Table' button, I run a BindData procedure to bind to the database via a SQLClient Connection an issue the following SQL statement.

 Dim sql = "SELECT * FROM " & tablename

It only shows the first table in the database, no matter what table from the drop-down I select.
I think I am not binding the DataSource property to the new dataset. I tried "GridView1.DataSource = DataSet" but got an error that "dataset is an object and could not use dataset as an expression."

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

DiskScannerAuthor Commented:
<Dim sql = "SELECT * FROM " & tablename

It only shows the first table in the database, no matter what table from the drop-down I select.>

Clarification on the above passage. My drop-down shows all the tables in the database, but my GridView only shows the first table.

- DiskScanner
DiskScannerAuthor Commented:
I think I might have discovered the solution myself. I am currently trying to use "User Controls" for each different table. Here is how I plan to attack.

Use my existing page with the drop-down list bound to the database. Then create "User Controls" for each table in my database complete with the database connectivity, stored procedures etc...  Then add all the user controls to the page and set the visibility property to "FALSE". Then when the user selects a certain table, make all other "User Controls" invisible, and make the currently selected table/"User Control" visible. So when the user selects "_tblCircuit", I would make all other "User Controls" invisible except the user control for "_tblCircuit". I hope this works. It may not be the BEST way, but I think it will work. I'll post back when/if it works.

- DiskScanner

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DiskScannerAuthor Commented:
It appears to be working. I will close this question.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.