I want to insert a new record into the table using the gridview. I can already edit and delete records from the table using the gridview.
Main Topics
Browse All TopicsI am looking for a way to insert a new record dierectly from the gridview. I have the delete and edit working fine and can add a new record if I also link a detail view to the gridview. Would be a very clean operation if adding a new record directly from the gridview were possible. If you know of a way, please provide "VB" sample code.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi,
check this article...i had same situation and it helped me.
first 2 link are for datagrid...but its one of the same thing so
this should help you solve your problem.
http://aspnet.4guysfromrol
http://www.codeproject.com
http://www.devx.com/dotnet
Let me know if you have any problem
Rugved
This is a big shortcoming in the GridView & the DetailsView is a bad solution.
Anyway, you can make it work by manually configuring columns and asp:TemplateField, using FooterView for each column. It's a pain and doesn't look great either, so you can add a button to show/hide the footer manually. This isn't my technique though, it and all the code is explained here: http://fredrik.nsquared2.c
There is a low-code method of dynamically adding a row containing a DetailsView to a GridView. It's not the best way I've seen to do this, but it's faster than the above if you have a ton of grids and little time: http://fredrik.nsquared2.c
I've recently had some luck just using an SqlDataSource with a UNION query in the SelectCommand, and a conditional UpdateCommand. In ASP.Net 2.0, no additional code is required.
SelectCommand:
SELECT ID, Data, MoreData FROM dbo.Items UNION Select Null, Null, Null ORDER BY Data;
This places a blank row as the first item in the GridView. Sorting in descending order will place it at the end.
UpdateCommand:
IF (@original_ID IS NULL) BEGIN INSERT INTO dbo.Items (Data) VALUES (@Data, @MoreData) END
ELSE BEGIN UPDATE dbo.Items SET Data=@Data, MoreData=@MoreData WHERE ID=@original_ID END;
Use buttons with Edit and Update CommandName actions as normal. When data is placed in bound controls for the empty row, clicking the update button willl insert it instead. When the primary key contains a value, a normal update process occurs. No further code is needed!
(Actually, some code can help highlight the blank row to distinguish it from existing data, Or simply use DataBinding to change the Text or ImageURL of the update button depending on whether the primary key is null or not.)
This works well with a SqlServer database; mileage may differ with other systems.
Business Accounts
Answer for Membership
by: rugu_16Posted on 2006-01-30 at 13:52:26ID: 15828405
Hi,
so let me understand this by adding new record do u mean you want to add a new line to gridview control
or do u want gridview control to appear in editable mode so you can directly edit them and save changes.
Rugved