ASP.NET - How To Add Serial Number Column To A GridView


To add a serial number column to a gridview , you would add the following markup  to the source code of the gridview


<asp:TemplateField HeaderText="S/N">
   <ItemTemplate>
     <asp:Label ID="lblRowNumber" Text='<%# Container.DataItemIndex+1 %>' runat="server" />
   </ItemTemplate>
</asp:TemplateField>




Below is what the markup for the gridview would look like .The aspect that handles the serial number generation is highlighted for emphasis.






<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="Employee_ID" DataSourceID="SqlDataSource1"
                    ForeColor="#333333" Width="341px">
   <AlternatingRowStyle BackColor="White" />
    <Columns>
     <asp:TemplateField HeaderText="S/N">
     <ItemTemplate>
       <asp:Label ID="lblRowNumber" Text='<%# Container.DataItemIndex+1 %>' runat="server" />
     </ItemTemplate>
     </asp:TemplateField>
                        <asp:BoundField DataField="Employee_ID" HeaderText="Employee_ID"
                            InsertVisible="False" ReadOnly="True" SortExpression="Employee_ID" />
                        <asp:BoundField DataField="FirstName" HeaderText="FirstName"
                            SortExpression="FirstName" />
                        <asp:BoundField DataField="Salary" HeaderText="Salary"
                            SortExpression="Salary" />
                    </Columns>
                    <EditRowStyle BackColor="#7C6F57" />
                    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#E3EAEB" />
                    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#F8FAFA" />
                    <SortedAscendingHeaderStyle BackColor="#246B61" />
                    <SortedDescendingCellStyle BackColor="#D4DFE1" />
                    <SortedDescendingHeaderStyle BackColor="#15524A" />

                </asp:GridView>







Previous
Next Post »
0 Komentar

ASP.NET - How To Display Animated .GIF On Postback

When designing an interactive web page, it is important for you to let the user know that  an action initiated by him/her is in progress.One...