Click here to Skip to main content
14,239,281 members
Rate this:
Please Sign up or sign in to vote.
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

Container.DataItemIndex  does not support gridview

use it <%# Container.ItemIndex+1 %>
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100