New to Telerik UI for ASP.NET AJAX? Start a free 30-day trial
Adding Row Numbers
In some occasions you may want to display row numbers in Telerik RadGrid- for example to correlate the grid items when exporting the control's structure to Excel or just for easier navigation/access. The functionality is not built-in in the product, however such layout is achievable with a few lines of code:
-
Subscribe to the ItemDataBound event of the grid
-
Check whether the currently bound item is GridDataItem
-
Extract the ItemIndex of the e.Item instance and display its value in a control residing in GridTemplateColumn's ItemTemplate.
In the example code below the item index is presented in MS Label control which is wrapped in template column:
ASP.NET
<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" DataSourceID="SqlDataSource1" AllowSorting="True"
Skin="Vista" runat="server" Width="95%" OnItemDataBound="RadGrid1_ItemDataBound">
<MasterTableView>
<Columns>
<telerik:GridTemplateColumn UniqueName="TemplateColumn" HeaderText="Row number">
<ItemTemplate>
<asp:Label ID="numberLabel" runat="server" Width="30px" />
</ItemTemplate>
<HeaderStyle Width="30px" />
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode] FROM [Customers]">
</asp:SqlDataSource>