Dynamically update ,delete ,insert GridView in ASP.NET
My database tables list in dropdownlist...when i select table from dropdown table display in GridView. I want to edit,delete & insert dynamically in GridView. Please give me sol开发者_StackOverflow中文版ution....
Check out these links:
- http://geekswithblogs.net/dotNETvinz/archive/2009/02/22/gridview-insert-edit-update-and-delete--the-ado.net-way.aspx
- http://msdn.microsoft.com/en-us/library/aa479339.aspx
Say you have three database tables, Customer, Orders and Products - do you mean the names of these tables appear in your dropdownlist?
If so, when a table name is selected in the dropdownlist (and perhaps an 'Edit' button is clicked), you'll need to bind your GridView to the selected table's data.
You could do this with inline SQL - build it from the DDL:
string _selectString = "SELECT * FROM " + ddlTables.SelectedValue ; //Remember to include the schema in the dropdownlist's value property
And then use this SQL to fetch back the data and bind your grid to it.
A better way would be to wrap up the SQL in a stored procedure that uses SQL Server's INFORMATION_SCHEMA
schema (which holds all the database's objects)
CREATE PROCEDURE MySchema.GetTableData
@TableName VARCHAR(Max),
@SchemaName VARCHAR(MAX) --Pass in the relevant Schema
AS
BEGIN
SET NOCOUNT ON
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = @TableName
AND TABLE_SCHEMA = @SchemaName
END
and get the data out this way. The only difference to the way you're probably already doing this is to set the SQLCommand's CommandType
property to CommandType.StoredProcedure
and to pass in the tablename and schema name as SQLParameters.
More information about ASP.Net and Stored Procedures:
http://www.c-sharpcorner.com/UploadFile/gtomar/storedprocedure12052007003126AM/storedprocedure.aspx
Once you've got the data from the table you just use the code & process linked to by @Brian.
hth.
精彩评论