0

I am creating a GridView that will initially have a blank row (consisting of 2 DropDownLists and one TextBox). There is a button in the footer that will allow a user to add another row after they have filled in the first. I am binding this to a SqlDataSource so I am able to use the InsertCommand to call a stored procedure to Insert the user's data into a database table. However, I am not sure how to go about adding an empty row. Right now, the headers will not show up until a postback call is executed. I have seen tutorials that use a DataTable to add an empty row, but I could not get this to work with the bound DropDownLists. Is there a way to add a row to the SqlDataSource from code behind? The following is the relevant part of my ASP page:

        <table>
        <tr>
        <td colspan="2" style="width:100%">
           <div>
            <asp:UpdatePanel ID="upUpdateChecklist" runat="server">
                <ContentTemplate>
                    <asp:GridView ID="gvUpdate" runat="server" AutoGenerateColumns="False"
                                  CssClass="mGrid" PagerStyle-CssClass="pgr"
                                  AlternatingRowStyle-CssClass="alt"
                                  ShowHeaderWhenEmpty="true"
                                  Visible="true" CellSpacing="2" 
                                CellPadding="2" ShowFooter="true"
                                DataSourceID="dsUpdate">
                        <Columns>
                            <asp:TemplateField HeaderText="Division/Context">
                                <ItemTemplate>
                                    <asp:DropDownList ID="ddDivision" runat="server" DataTextField="Division" DataValueField="ID" DataSourceID="dsDivision"/>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Application">
                                <ItemTemplate>
                                    <asp:DropDownList ID="ddApplication" runat="server" DataTextField="Application" DataValueField="ID" DataSourceID="dsApplication" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Task">
                                <ItemTemplate>
                                    <asp:TextBox ID="tbTask" runat="server" ReadOnly="false" />
                                </ItemTemplate>
                            </asp:TemplateField>

                            <asp:TemplateField>
                            <FooterStyle HorizontalAlign="Right" />
                            <FooterTemplate>
                                <asp:Button ID="btnAdd" Text="Add New Row" runat="server" />
                            </FooterTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </ContentTemplate>
            </asp:UpdatePanel>
          </div>
        </td>
        </tr>
        </table>
       <asp:SqlDataSource ID="dsApplication" SelectCommand="SELECT ID, ApplicationName FROM Automation.dbo.Applications"
                          runat="server"
                          ConnectionString="<%$ ConnectionStrings:AutomationDBConnectionString %>"></asp:SqlDataSource>

       <asp:SqlDataSource ID="dsDivision" SelectCommand="SELECT ID, Division FROM Automation.dbo.Division ORDER BY Application, Division"
                          runat="server"
                          ConnectionString="<%$ ConnectionStrings:AutomationDBConnectionString %>"></asp:SqlDataSource>
       <asp:SqlDataSource ID="dsUpdate" runat="server"
                          ConnectionString="<%$ ConnectionStrings:AutomationDBConnectionString %>"
                          SelectCommand="">
                          <InsertParameters>
                            <asp:Parameter Name="Division" DbType="Int32" />
                            <asp:Parameter Name="Application" DbType="Int32" />
                            <asp:Parameter Name="Task" DbType="String" />
                          </InsertParameters>
                          </asp:SqlDataSource>
2
  • Why not remove that functionality from a GridView (which is designed to display data, not empty rows), and put it in a Panel which you can clear after each row insert. Commented Jul 31, 2013 at 21:30
  • I am using a GridView because I want to do one bulk insert as opposed to an insert for each entry. This way, a user will see all the records they wish to insert before they choose to click a button to perform said insert. Commented Jul 31, 2013 at 21:35

1 Answer 1

1

I am not completely sure I understand how do you intend to achieve what you want but if you want to generate an empty row, change the select command of the SQL data source to do a union with an empty dummy row.

   <asp:SqlDataSource ID="dsApplication" SelectCommand="SELECT ID, ApplicationName FROM Automation.dbo.Applications UNION select -1 as ID, '' as ApplicationName "
Sign up to request clarification or add additional context in comments.

1 Comment

I edited my initial question. I am not opposed to binding the GridView to a DataTable, but I am not exactly sure how to do this with 2 of the GridView Columns being Bound DropDownLists (to other data sources).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.