Click here to Skip to main content
15,947,958 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i want to auto generate serial no . in data grid
I am using this code
<asp:DataGrid ID="dgdefaulertlist" runat="server" Width="85%" AutoGenerateColumns="false"
                   BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" AllowPaging="True" PageSize="40"  Height="10%"  OnPageIndexChanged="dgdefaulertlist_PageIndexChanged" AllowSorting="True" >
                       <asp:TemplateColumn HeaderText = "Serial No.">
                       <asp:Label ID = "lblserial" Text ='<%# Container.DataItemIndex + 1%>' runat= "server"></asp:Label>
                    <asp:BoundColumn HeaderText="Date" DataField="startdate"></asp:BoundColumn>
                    <asp:BoundColumn HeaderText="Employee Name" DataField="employeename"></asp:BoundColumn>
                   <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                   <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                   <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" Mode="NumericPages" />
                   <ItemStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                   <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                   <AlternatingItemStyle BackColor="#F7F7F7" />

it gives error
'System.Web.UI.WebControls.DataGridItem' does not contain a definition for 'DataItemIndex'

Thanks in Advance

1 solution

Container.DataItemIndex  does not support gridview

use it <%# Container.ItemIndex+1 %>
Share this answer
Rezwan Ul 11-Sep-12 23:18pm    
1. Please use <%# Container.DataItemIndex + 1 %> for asp:GridView
2. and use <%# Container.ItemIndex + 1 %> for asp:DataGrid.

Thank you Sudip Saha

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900