Andrew
Andrew

Reputation: 811

Adding Blank Rows to GridView bound to SqlDataSource

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>

Upvotes: 0

Views: 1889

Answers (1)

Icarus
Icarus

Reputation: 63966

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 "

Upvotes: 1

Related Questions