Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » WebForms Controls » Inserting new row in GridView in ASP.NET 2.0

Inserting new row in GridView in ASP.NET 2.0

The GridView was not designed to insert new rows, but there is a way to accomplish this with very little code. This article shows how to do that.

Page Views : 316884
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Nevron Chart
Become a Sponsor
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

The GridView was not designed to insert new rows, but there is a way to accomplish this with very little code.

  

  1. The first step is to add your data source and the DataView Grid. Ensure that your SqlDataSource statements or stored procedures in the:

    1. Select Command
    2. Insert Command
      1. My stored procedure just adds a new row and initializes to new or zero
    3. Update Command
    4. Delete Command

  2. Bind the DataView Grid to the SqlDataSource

  3. Edit the fields adding the following

    1. CommandField, enable the Edit and Delete, and disable the insert
    2. TemplateField



  4. In the UI source code, I inserted the Item Template as follows:

    <ItemTemplate>

      <asp:LinkButton ID="lblNew" runat="server" CausesValidation="false"

                  CommandName="Insert"

                  Text="New" style="vertical-align: middle; text-align:

                  center" Font-Bold="False" ForeColor="Black"

                  Height="3px" Width="49px"

        OnClick="GridViewInsert">

      </asp:LinkButton>

    </ItemTemplate> 

  5. Format the text as required, leaving the rest alone.

    Text
    ="New" style="vertical-align: middle; text-align: 
    center" Font-Bold="False" ForeColor="Black" 
    Height="3px" Width="49px"

  6. Now your control looks as follows in design mode



  7. The next step is to add a few lines of code so that there is only one New, when the web page is running

  8. In the page where the control is running, insert the following code and change the names as required:

    protected void GridViewInsert(object sender, EventArgs e)
    {
            SqlDataSource1.Insert();
    }
    protected void GridView1_PreRender(object sender, EventArgs e)
    {
           for (int index = 0; index < GridView1.Rows.Count - 1; index++)
            {

                GridView1.Rows[index].Cells[1].Controls[1].Visible = false;

            }

    }

  9. Test and change the Cells[x] and Controls[x] as required

  10. There will be training for the users, the User Sequence is:

    1. The user presses the New button
      1. The stored procedure inserts New or zero into each field
    2. The user now sees a new row at the bottom with New
    3. The user edits this new row, changing the values as required.
    4. The user presses the Update

The user pressed the Edit button

The user entered data and pressed the Update button

So how did this work, well in step 4 above, the OnClick="GridViewInsert" above calls the GridViewInsert(object sender, EventArgs e) code and executes the SqlDataSource1.Insert();, which inserts the data. Getting rid of all of the News in the DataView (except for the last one), happened in the GridView1_PreRender code.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 Article Extensions
Contents added by nguyen chien on Nov 12, 2009
aaaaaaaaa
Contents added by nguyen chien on Nov 12, 2009
Download File: BankingSystemAHCode.zip
good
 [Top] Rate this article
 
 About the author
 
art_scott
Senior Lead Architect/Developer utilizing .NET C# 2.0/3.0, ASP.NET, SQL Server 2005, Reporting Server, .NET 3.0 Workflow, and Web Services. methodologies are Extreme Programming on the Microsoft Enterprise Library utilizing Model View Presenter (MVP). I utilize Visio Architecture to create UML which is then used to update the Team Visual Studio 2005. As the architect and senior/lead developer ensuring design standards, I also train my team on MVP development (WinForms and WebForms), pattern development, C# 2.0/3.0 development and coding as a working in the coding trenches while taking the time to meet with clients on a regular basis and ensuring that our developers are not only being taken care of but excited about our technology and methodologies.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
Info Grid View by ravikanth On December 10, 2007
I want a get a new row created when user clicks addnew button that is wityh out binding to data source. To be clear when user see the page Grid view must contain only columns and by adding button a new row should get created and after entring values and clicking on Update button Grid view should get updated with new row. Please help me regarding this
Reply | Email | Modify 
how to edit,delete recorde in grideview by ambrish On January 13, 2008
how to edit,delete recorde in grideview
Reply | Email | Modify 
Adding Dynamic Row in GridView by Bhaskar On January 15, 2008
Hello friends I wan to add Adding Dynamic Row in GridView in C# and ASP.Net so please reply me Thanks Bhaskar
Reply | Email | Modify 
Insert Row GridView by Keith On February 13, 2008
I think you have missed a line of code in the article. Need to directly appoint an Event Handler for GridView1_PreRender : this.GridView1.PreRender += this.GridView1_PreRender; and put this in the Page_Load Event Handler
Reply | Email | Modify 
Insert Row GridView by Keith On February 13, 2008
I think you have missed a line of code in the article. Need to directly appoint an Event Handler for GridView1_PreRender : this.GridView1.PreRender += this.GridView1_PreRender; and put this in the Page_Load Event Handler
Reply | Email | Modify 
Insert(); ???? by pari On April 23, 2010
If it is stored procedure then how it will work?ur method is easy n good but plz describe this function? 
Reply | Email | Modify 
Does not work with empty data source by Uyduruk On December 14, 2010
This does not work with empty data source as there are no rows to be displayed and new link is not visible.
Reply | Email | Modify 
Want to insert a new row in grid view by Shashank On April 11, 2011
I hav 2 droup down list and 3 text boxes and on clicking a button i want that a new roe inserted in grid view.
Reply | Email | Modify 
DevExpress Free UI Controls
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.