[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

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

Hello,
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.)

Thanks,

DiskScanner.
0
DiskScanner
Asked:
DiskScanner
  • 4
1 Solution
 
divinewind80Commented:
Assuming you are using SQL Server, you can use the following PAQ to see how to list all your tables:

http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_21602458.html

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?
0
 
ReecioCommented:
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?
0
 
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."

DiskScanner
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
0
 
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
0
 
DiskScannerAuthor Commented:
It appears to be working. I will close this question.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now