Sunday, November 22, 2015

Gridview in asp.net

Grid View Control:

It is used for data binding in tabular format.

<asp:gridview ID="Gridview1" runat="server">
    <Columns>
        <asp:BoundField DataField="" HeaderText ="" />               
        <asp:TemplateField>
            <HeaderTemplate></HeaderTemplate>
            <ItemTemplate></ItemTemplate>
            <FooterTemplate></FooterTemplate>                  
        </asp:TemplateField>
    </Columns>
</asp:gridview>


There are multiple section in Gridview  in <Columns> section:
<asp:BoundField />
<asp:TemplateField />
<asp:ButtonField />
<asp:CheckBoxField />
<asp:HyperLinkField />
<asp:CommandField />                       
<asp:ImageField />


1. In BoundField, We provide column name in DataField. We set header display text in HeaderText.

Example:

       <asp:BoundField DataField="EmpName" HeaderText="Employee Name"
                FooterStyle-Font-Bold="true"
                ItemStyle-HorizontalAlign="right"                
                HeaderStyle-HorizontalAlign="right"
                HeaderStyle-Width="300"
                FooterText ="Employee Name"/>


2. Template Field is used for display column in customised way. In TemplateField,  We can set any numbers of controls. It has three main section-

       a.     <HeaderTemplate></HeaderTemplate>

              In HeaderTemplate, we can do all custom setting for header.

              For header style for a particular column we use <HeaderStyle />

       b.  <ItemTemplate></ItemTemplate>

              In ItemTemplate, we can do all setting related to the data shown.

              For item style for a particular column we use <ItemStyle />

       c.     <FooterTemplate></FooterTemplate>

              In FooterTemplate, We can do all setting that will come in footer of the table (Gridview).


       Example :

                     <asp:TemplateField>
                <HeaderTemplate>Address</HeaderTemplate>
                <HeaderStyle BackColor="Blue" ForeColor="White" />

                <ItemTemplate>
                    <asp:Label ID="lblAddress" runat="server" Text='<%# Bind("Address") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle BackColor="Green" ForeColor="White" />

                <FooterTemplate>Address</FooterTemplate>
                <FooterStyle BackColor="Blue" ForeColor="White" />
            </asp:TemplateField>

      
Note 1 : Footer will shown only if we have set ShowFooter="true" in Gridview properties.
Note 2 : AllowPaging="true" is used to show paging in gridview. Set PageSize="10" value for change default PageSize (10) in Gridview properties.

Note 3 : Set AutoGenerateColumns="false" , when we want to define gridview columns in                                 customised way.

Note 4 : For databind in a control in design (.aspx) page we use following method Eval, Bind                   method. Note the "<%# ....... %>"  it is used for databind in design page.

              Example:
              <asp:Label ID="lblAddress" runat="server" Text='<%# Eval("Address") %>' ></asp:Label>
              <asp:Label ID="lblAddress" runat="server" Text='<%# Bind("Address") %>' ></asp:Label>