This is a migrated thread and some comments may be shown as answers.
Auto generated Row Number in RadGrid
6 Answers 181 Views
This is a migrated thread and some comments may be shown as answers.
Rajeev
Top achievements
Rank 1
Rajeev asked on 04 Mar 2014, 03:13 PM
Dear Friend,

I am creating a entry screen using Telerik Rad Grid in VS 2012. I am using " Batch Edit" mode  of grid. I have requirement of few items whenever user will add row or edit column value. My requirements are...

1. My first column is "Row Number" in grid and I want to generate one sequential number (1,2,3...) automatically when user will add new row.
2. I have 3 columns (price,  number of items and Total Price). I want to automatically generate total Price whenever user will update price and number of items in columns. 

It's very urgent requirement so appreciating your fast response.  

Thanks & Regards,
Raj

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 05 Mar 2014, 07:47 AM
Hi Rajeev,

Please try the following code snippet which shows your described scenarios.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AllowAutomaticDeletes="True" AllowAutomaticInserts="true" AllowAutomaticUpdates="True" AllowPaging="True" AutoGenerateColumns="False" Skin="Outlook" DataSourceID="SqlDataSource1" OnItemDataBound="RadGrid1_ItemDataBound">
    <MasterTableView CommandItemDisplay="Bottom" DataKeyNames="OrderID" InsertItemDisplay="Bottom" EditMode="Batch" AutoGenerateColumns="False" CommandItemSettings-ShowSaveChangesButton="true"
        CommandItemSettings-ShowRefreshButton="false">
        <Columns>
            <telerik:GridTemplateColumn UniqueName="TemplateColumn" HeaderText="Row number">
                <ItemTemplate>
                    <asp:Label ID="numberLabel" runat="server" Width="40px" />
                </ItemTemplate>
                <HeaderStyle Width="100px" />
            </telerik:GridTemplateColumn>
            <telerik:GridBoundColumn FilterControlAltText="Filter OrderID column" UniqueName="OrderID" HeaderText="OrderID" DataField="OrderID" ReadOnly="false">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn FilterControlAltText="Filter ShipName column" UniqueName="ShipName" HeaderText="ShipName" DataField="ShipName" ReadOnly="false">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn FilterControlAltText="Filter Freight column" UniqueName="Freight" HeaderText="Freight" DataField="Freight">
            </telerik:GridBoundColumn>
            <telerik:GridCalculatedColumn UniqueName="CalculatedColumn" HeaderText="Calculated Column" DataFields="OrderID, Freight" Expression='{0} +{1}'>
            </telerik:GridCalculatedColumn>
            <telerik:GridClientDeleteColumn UniqueName="delete" HeaderText="Delete">
            </telerik:GridClientDeleteColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind_newConnectionString3 %>"
    DeleteCommand="DELETE FROM [Orders] WHERE [OrderID] = @OrderID" InsertCommand="INSERT INTO [Orders] ([OrderID], [ShipName], [Freight]) VALUES (@OrderID, @ShipName, @Freight)"
    SelectCommand="SELECT [OrderID], [ShipName],[Freight] FROM [Orders] " UpdateCommand="UPDATE [Orders] SET [ShipName] = @ShipName, [Freight] = @Freight WHERE [OrderID] = @OrderID">
    <DeleteParameters>
        <asp:Parameter Name="OrderID" Type="Int32"></asp:Parameter>
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="OrderID" Type="Int32"></asp:Parameter>
        <asp:Parameter Name="ShipName" Type="String"></asp:Parameter>
        <asp:Parameter Name="Freight"></asp:Parameter>
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="OrderID" Type="Int32"></asp:Parameter>
        <asp:Parameter Name="ShipName" Type="String"></asp:Parameter>
        <asp:Parameter Name="Freight"></asp:Parameter>
    </UpdateParameters>
</asp:SqlDataSource>

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{   
  //To add row number
  if (e.Item is GridDataItem)
  {
    int rowCounter = new int();
    Label lbl = e.Item.FindControl("numberLabel") as Label;
    rowCounter = RadGrid1.MasterTableView.PageSize * RadGrid1.MasterTableView.CurrentPageIndex;
    lbl.Text = (e.Item.ItemIndex + 1 + rowCounter).ToString();
  }
}

Thanks,
Princy
0
Rajeev
Top achievements
Rank 1
answered on 05 Mar 2014, 08:46 AM
Thanks for your response. This is helpful for me but I am looking this functionality when user performing data entry in grid. Your provided solution is useful when I am binding DB data in grid. Could you please suggest better option in my scenario?

Thanks & Regards,
Raj
0
Princy
Top achievements
Rank 1
answered on 06 Mar 2014, 08:56 AM
Hi Rajeev,

Please take a look at this forum which discuss a similar scenario.
http://www.telerik.com/forums/calculatedcolumn-in-batch-editing#E_StRdSQbk65RJSxMijNww

Thanks,
Princy
0
Sushilkumar
Top achievements
Rank 1
answered on 20 Jan 2016, 12:26 PM

try this

if (e.Item is GridDataItem)
        {
            Label lbl = e.Item.FindControl("numberLabel") as Label;
            lbl.Text = (e.Item.ItemIndex + 1 + (rgdNPDChecklist.MasterTableView.PageSize * rgdNPDChecklist.MasterTableView.CurrentPageIndex)).ToString();
        }

0
chinnu Blessen
Top achievements
Rank 1
answered on 24 Feb 2016, 11:54 AM

Simply use the below code

<telerik:GridTemplateColumn UniqueName="TemplateColumn" HeaderText="Row number">
                <ItemTemplate>
                    <asp:Label ID="numberLabel" runat="server" 

Text='<%#Container.ItemIndex+1%>' Width="40px" />
                </ItemTemplate>
                <HeaderStyle Width="100px" />
            </telerik:GridTemplateColumn>

0
roopsundar
Top achievements
Rank 1
answered on 18 Aug 2017, 02:41 PM
This works fine... thanks..
Tags
Grid
Asked by
Rajeev
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Rajeev
Top achievements
Rank 1
Sushilkumar
Top achievements
Rank 1
chinnu Blessen
Top achievements
Rank 1
roopsundar
Top achievements
Rank 1
Share this question
or