This is a migrated thread and some comments may be shown as answers.

row number as a column.

2 Answers 83 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joslyn
Top achievements
Rank 1
Joslyn asked on 08 Nov 2013, 01:22 PM
Hi
 how to add row number as a column.

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 08 Nov 2013, 01:26 PM
Hi Joslyn,

Please try the following code snippet to set row number.

ASPX:
<telerik:GridTemplateColumn UniqueName="TemplateColumn" HeaderText="Row number">
    <ItemTemplate>
        <asp:Label ID="numberLabel" runat="server" Width="30px" />
    </ItemTemplate>
    <HeaderStyle Width="30px" />
</telerik:GridTemplateColumn>

C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridDataItem && e.Item.OwnerTableView.DataSourceID == "SqlDataSource1")
    {
        Label lbl = e.Item.FindControl("numberLabel") as Label;
        lbl.Text = (e.Item.ItemIndex + 1).ToString();
    }
}

Thanks,
Shinu
0
Konstantin Dikov
Telerik team
answered on 11 Nov 2013, 03:08 PM
Hi Joslyn,

In addition to Shinu's approach you could also achieve this entirely in the markup with the following GridTemplateColumn:
<telerik:GridTemplateColumn UniqueName="TemplateColumn" HeaderText="Row">
    <ItemTemplate>
        <asp:Label ID="numberLabel" runat="server" Width="30px" Text='<%# (Container as GridItem).ItemIndex + 1 %>' />
    </ItemTemplate>
    <HeaderStyle Width="30px" />
</telerik:GridTemplateColumn>

Hope that  helps.

 

Regards,
Konstantin Dikov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Joslyn
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or