Datagrid / DataList to create table with an "add record" input row (VB)

Posted on 2008-11-20
Last Modified: 2012-05-05
I have (or should say had) a datagrid (before I swapped it for a datalist) which is databound to a datasource.
I want to have the effect of a table containing a header, the rows of data and a single "add record" row with textboxes and an "add" button.
How do I do this or achieve the same effect?


I swapped over to a datalist with header/footers, thinking I could move my textbox inputs into the footer but the a) the code does not validate and b) the resulting output adds in additional <table> tags where I don't want them.
Question by:Beamson
    1 Comment
    LVL 16

    Accepted Solution

    I use an SqlDataSource to databind a GridView to do this.

    (1) Use a Union Query in the Select Command to provide a blank row.   Include a flag to identify this (which also helps with sorting).  

    (2) Use the flag to determine if you need to insert or update in the UpdateCommand.

    (2b) The InsertCommand is not used in this senario, it's all done by the UpdateCommand.  Simply use the Edit and Update CommandNames as for any other row.  I typically use the IsNewRow flag to select the appropriate image button URL and AlternateText.  It's handy if I need to do any extra formatting in the OnDataBound event to distinguish the row.

    <asp:SqlDataSource ID=Sample runat=server
        ConnectionString='<%$ ConnectionStrings:YourConnectionString %>'
        SelectCommand="SELECT DataKey, DataValue, 0 As IsNewRow FROM KeyValues 
            UNION SELECT NULL As DataKey, NULL As DataValue, -1 As IsNewRow 
            ORDER BY IsNewRow, DataValue;"
        UpdateCommand="IF (@oldIsNewRow = -1) BEGIN
            INSERT INTO KeyValues (DataKey, DataValue) VALUES (@DataKey, @DataValue);
            END ELSE BEGIN
            UPDATE KeyValues SET DataKey=@DataKey, DataValue=@DataValue WHERE DataKey=@oldDataKey;
            <asp:Parameter Name=oldIsNewRow>
            <asp:Parameter Name=oldDataKey>
            <asp:Parameter Name=DataKey>
            <asp:Parameter Name=DataValue>

    Open in new window


    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now