Good afternoon,

I have a problem... Else I wouldn't be posting here, right?

My quandary is thus:

I have an empty table in a database that I want a user to be able to fill. This will occur when employees are replaced for sick leaves, etc.

I'd like to be able to have them populate (insert) information into the table, where I can then match it into another table that I have for a report.

I've been looking up using GridView ... a lot ... and it's kind of helping me get back into the swing of things code-wise, but I can't, for the life of me, get a footer row to display text boxes so that I can add information to the table.

I barely have any code-behind for the page_load event, no subs written ... Just the GridView code.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            CellPadding="4" DataSourceID="SCHOOL_LISTING" ForeColor="#333333" 
            GridLines="None" AllowSorting="True" AutoGenerateEditButton="True"
            showheaderwhenempty="True" ShowFooter="true">
            <EmptyDataRowStyle BackColor="lightblue" ForeColor="Red" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:TemplateField HeaderText="EMPL_MATR" SortExpression="EMPL_MATR">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("EMPL_MATR") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="EMPL_FIRST" SortExpression="EMPL_FIRST">
                    <FooterTemplate>
                        <asp:TextBox ID="EmployeeFirstName" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("EMPL_FIRST") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="EMPL_LAST" SortExpression="EMPL_LAST">
                    <FooterTemplate>
                        <asp:TextBox ID="EmployeeLastName" runat="server"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("EMPL_LAST") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="REPLACING" SortExpression="REPLACING">
                    <FooterTemplate>
                        <asp:TextBox ID="EmployeeReplacedBy" runat="server" style="margin-bottom: 0px"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("REPLACING") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField HeaderText="Test" NullDisplayText="N/A" DataField="EMPL_MATR" />
                <asp:TemplateField Visible="true">
                    <FooterTemplate>
                        <asp:Button ID="AddEmployee" runat="server" Text="Update" />
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />        
        </asp:GridView>
        <asp:SqlDataSource ID="SCHOOL_LISTING" runat="server" 
            ConnectionString="<%$ ConnectionStrings:PHONE_LISTConnectionString %>" 
            SelectCommand="SELECT * FROM [REPLACEMENT]" InsertCommand="INSERT INTO [REPLACEMENT](EMPL_MATR, EMPL_FIRST, EMPL_LAST, REPLACING) 
            VALUES(@EMPL_MATR, @EMPL_FIRST, @EMPL_LAST, @REPLACEMENT)">
            <insertparameters>
                <asp:Parameter Type="String" Name="EMPL_MATR" />
                <asp:Parameter Type="String" Name="EMPL_FIRST" />
                <asp:Parameter Type="String" Name="EMPL_LAST" />
                <asp:Parameter Type="String" Name="REPLACEMENT" />
            </insertparameters>
        </asp:SqlDataSource>

You can see that I have the Footer row in there, with an "add" button ... but whenever I debug, I do not get a Footer row at all. Nothing. Zip. I get a Header row, but no Footer.

I've even scratched the webapplication, and started over... Same result.

Anyone be able to help?

Dani AI

Generated

Short diagnosis: ’s markup shows the header (ShowHeaderWhenEmpty) but no footer because GridView does not render a FooterRow for a completely empty result set. Setting ShowFooter="true" only causes a footer to appear when the control actually has data rows; there’s no built‑in ShowFooterWhenEmpty toggle. (learn.microsoft.com)

Practical approaches (pick one that fits the app):

  • EmptyDataTemplate: define the insert UI (TextBoxes + Add button) inside the GridView’s EmptyDataTemplate so an insert form appears when the resultset is empty; wire the button to call the data source Insert logic. (learn.microsoft.com)

  • Dummy row workaround: if keeping FooterTemplate is preferred, add a harmless dummy row to the data source before calling DataBind so the GridView renders a FooterRow; then detect that dummy row in RowDataBound and hide it so only the footer shows. This is a common workaround when an empty dataset must still render footer controls. (stackoverflow.com)

  • Use a templated control (ListView/FormView): those controls offer InsertItemTemplate / Insert support and make inline inserts cleaner than forcing the GridView to behave when empty. (learn.microsoft.com)

Example pattern for handling the Add button in the footer (C#): bind the button to a server click (or use RowCommand), pull values from the FooterRow controls into the SqlDataSource InsertParameters, call Insert(), then rebind.

protected void AddEmployee_Click(object sender, EventArgs e)
{
    var f = GridView1.FooterRow;
    if (f == null) return; // footer not rendered when there are no data rows

    var first = f.FindControl("EmployeeFirstName") as TextBox;
    var last  = f.FindControl("EmployeeLastName") as TextBox;
    var repl  = f.FindControl("EmployeeReplacedBy") as TextBox;

    SCHOOL_LISTING.InsertParameters["EMPL_FIRST"].DefaultValue = first?.Text.Trim() ?? "";
    SCHOOL_LISTING.InsertParameters["EMPL_LAST"].DefaultValue  = last?.Text.Trim() ?? "";
    SCHOOL_LISTING.InsertParameters["REPLACEMENT"].DefaultValue = repl?.Text.Trim() ?? "";

    SCHOOL_LISTING.Insert();
    GridView1.DataBind();
}

Extra notes: ’s DataTable binding example shows how binding can be done in code — the same pattern can be adapted to add a single dummy row when Select returns zero rows. Avoid changing DataSourceID during RowDataBound; manipulate the data before the GridView is bound to prevent HttpExceptions. (learn.microsoft.com)

Recommended Answers

All 4 Replies

Know a couple of people using Iron Speed for web app dev. They say it supports import/export off the shelf. Not sure I really belive this, since it probably leads to more work in the long run...Good luck.

Is there nobody out there who has had this sort of project to work on?

This code may help you check your Gridview binding.

DataTable dt = new DataTable();

        dt.Columns.Add(new DataColumn("Item #", typeof(int)));
        dt.Columns.Add(new DataColumn("Contract Number", typeof(string)));
        dt.Columns.Add(new DataColumn("Customer Name", typeof(string)));

        int i;
        for (i = 0; i < 40; i++)
        {
            DataRow dr = dt.NewRow();
            dr["Item #"] = i;


            dr["Customer Name"] = this.txtCustomerName.Text;
            dr["Contract Number"] = this.txtContractNumber.Text;
            dt.Rows.Add(dr);
        }

        this.GridView1.Visible = true;
        GridView1.DataSource = dt;
        GridView1.DataBind();

Thanks ciint ... I've since decided to develop a small VB program, as it seems more conducive to what I need.

Thanks for the help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.