how to auto generate serial no. in data grid

Posted by Mayankshri under ASP.NET on 2/24/2012 | Points: 10 | Views : 8087 | Status : [Member] | Replies : 3
Hi, i want to auto generate serial no . in data grid
I am using this code
Collapse | Copy 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" >
<Columns>
<asp:TemplateColumn HeaderText = "Serial No.">
<ItemTemplate>
<asp:Label ID = "lblserial" Text ='<%# Container.DataItemIndex + 1%>' runat= "server"></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="Date" DataField="startdate"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Employee Name" DataField="employeename"></asp:BoundColumn>
</Columns>
<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" />
</asp:DataGrid>



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

Thanks in Advance




Responses

Posted by: Pradeepkumar417 on: 2/24/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Auto Generate Row Number is possible with Grid view only. I think you are developing the application in vs 2005 so there is no Gridview control,it is available in higher versions. So it is better to create a field in your table and make it as primary key. then you have get this field and bind to Datagrid

Thanks & Regards,
Software Engineer,
Pradeep Kumar

Mayankshri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Blessyjees on: 2/24/2012 [Member] Bronze | Points: 25

Up
0
Down
hi,
You can generate row number from sql query using ROW_NUMBER() function.

SELECT FirstName, LastName, ROW_NUMBER() OVER(ORDER BY FirstName DESC) AS 'Row Number'

FROM Employee


Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Mayankshri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 2/24/2012 [Member] Silver | Points: 25

Up
0
Down


Hi ,
Additionally try this.
http://www.extremeexperts.com/Net/CodeSnippets/GeneratingSeqNumber2.aspx

Thanks,
Sanjay

Mayankshri, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response