Shaokan
Shaokan

Reputation: 7684

gridview like database

How can I create a gridview control in c# asp.net which acts like an mssql database? I mean, I have a dropdownlist which the items represent the table names in the database and the gridview is constructed on the selected table name. So basically, the gridview display data from whatever table is selected in the dropdownlist. I want to edit, insert or delete rows from the database using this gridview. Any suggestions?

Upvotes: 1

Views: 610

Answers (4)

Shaokan
Shaokan

Reputation: 7684

As I could not find a solution to this problem I've decided to create a table dynamically and create the update / insert commands referencing some rows in the table.

Upvotes: 0

Vir
Vir

Reputation: 1354

In a gridview You can easily Bind all the values of a table by the use of the Query.

string selectSQL = String.Format("SELECT * FROM [{0}]", ddlTable.SelectedItem.Text);

just set AutoGenerateColumns="True"

now in the gridview you can enable the Command field in it will help you to this you can find in Column Property of GridView. By using this You can Edit Delete select any row in the GridTable.

<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowSelectButton="True" />
<asp:CommandField ShowDeleteButton="True" />  

Upvotes: 2

YetAnotherUser
YetAnotherUser

Reputation: 9356

You'll need to create a dynamic query using the selected table name from drop down. Execute it against the database and bind the results back to the gridview.

See this example for an example

You'll need to do something like this-

    string connectionString = WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
    string selectSQL = String.Format("SELECT * FROM [{0}]", ddlTable.SelectedValue);

    //execute query, fill dataset

    GridView1.DataSource = ds;
    GridView1.DataBind();

Also, you may want to see how to get List of all tables in database

Upvotes: 0

painotpi
painotpi

Reputation: 6996

If you want to generate the list of tables dynamically in your ddl then you can have a look here, You can also make use of this link, which will help you to INSERT,DELETE,UPDATE with the gridView.

Hope it helped :)

Upvotes: 0

Related Questions