Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add footer to gridview

Tags:

asp.net

I have a grid with 3 columns - Edit, ID, Movie. I would like to add a footer with an Insert link button, 2 textboxes respectively, but unable to do so. Is it possible.

ASPX:

 <asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="False"     
 OnRowEditing="gridview1_RowEditing" 
 OnRowCancelingEdit="gridview1_RowCancelingEdit"
    ShowFooter="true" >
    <Columns>
     <asp:CommandField ShowEditButton="true" ShowDeleteButton="true" />
      <asp:BoundField DataField="id" HeaderText="ID" />
      <asp:BoundField DataField="movie" HeaderText="MOVIE" />
     </Columns>
</asp:GridView>

When I try the following, there is an error for commandfield which says, this element is not supported.

<Columns>
 <asp:TemplateField>
  <ItemTemplate>
    <asp:CommandField ShowEditButton="true" ShowDeleteButton="true" />
  </ItemTemplate>
 <FooterTemplate>
    <asp:LinkButton ID="lnkInsert" runat="server" Text="Insert"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</columns>

The other way would be to use itemTemplate & EditTemplate for each column control. But I find this simple and would like to proceed this way. So Can I add a footer to this structure. enter image description here

like image 910
Ruby Avatar asked Sep 15 '13 16:09

Ruby


People also ask

How display total number of records in GridView?

In order to implement Paging in GridView, AllowPaging property is set to true and OnPageIndexChanging event has been handled. Below the GridView, there is a Label control which will be used to display the Count of Total number of records in the GridView.

What is Grid view in asp net?

What is ASP.NET GridView? GridView is a control used to display data in tables on a web page. It displays data in both rows and columns, where each column represents a field, and each row represents a record. GridView helps to perform key activities like Insert, Delete, Sorting, and Paging.


1 Answers

YES, It is possible. But this will require using the <FooterTemplate> inside <TemplateField>. Use TemplateFields for each of the columns and also set the FooterTemplate for each of the columns.

NOTE: The ID column seems here to be a Primary Key. So remove the <FooterTemplate> from the corresponding <TemplateField> defined for ID column, if ID is a Primary Key OR an autogenerated field in your database.

NOTE II: The <FooterTemplate> simply will contain a TextBox only.

<Columns>
<asp:TemplateField>
    <EditItemTemplate>
          <asp:LinkButton ID="lnkBtnUpdate" runat="server" CausesValidation="True"
                         CommandName="Update" Text="Update"></asp:LinkButton>
                         &nbsp;<asp:LinkButton ID="lnkBtnCancel" runat="server"
                         CausesValidation="False"
                         CommandName="Cancel" Text="Cancel">
          </asp:LinkButton>
    </EditItemTemplate>
    <FooterTemplate>
          <asp:LinkButton ID="lnkBtnInsert" runat="server"  
               CommandName="Insert">Insert</asp:LinkButton>
    </FooterTemplate>
    <ItemTemplate>
          <asp:LinkButton ID="lnkBtnEdit" runat="server" CausesValidation="False"
                          CommandName="Edit" Text="Edit"></asp:LinkButton>
                          &nbsp;<asp:LinkButton ID="lnkBtnDelete" runat="server"
                          CausesValidation="False"
                          CommandName="Delete" Text="Delete">
          </asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="ID">
    <EditItemTemplate>
          <asp:TextBox ID="TextBoxID" runat="server" Text='<%# Bind("ID") %>'>
          </asp:Label>
    </EditItemTemplate>
    <ItemTemplate>
          <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'>
          </asp:Label>
    </ItemTemplate>
    <FooterTemplate>
      <asp:TextBox ID="txtID" runat="server"></asp:TextBox>
    </FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MOVIE">
    <EditItemTemplate>
          <asp:TextBox ID="TextBoxMovie" runat="server" Text='<%# Bind("Movie") %>'></asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
          <asp:TextBox ID="txtMovie" runat="server"></asp:TextBox>
    </FooterTemplate>
    <ItemTemplate>
          <asp:Label ID="Label2" runat="server" Text='<%# Bind("Movie")%>'>
          </asp:Label>
    </ItemTemplate>
</asp:TemplateField>

Now there are 2 ways to insert Data. Either you can use GridView OnRowCommand event or you can handle the OnClick event of your Insert button.

like image 146
R.C Avatar answered Sep 29 '22 00:09

R.C