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 gridv开发者_StackOverflow社区iew 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?
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" />
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 :)
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
- DataBinding to GridView
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
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.
精彩评论