Auto generated Row Number in RadGrid

6 posts, 0 answers
  1. Rajeev
    Rajeev avatar
    3 posts
    Member since:
    Oct 2013

    Posted 04 Mar 2014 Link to this post

    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
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 05 Mar 2014 in reply to Rajeev Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Rajeev
    Rajeev avatar
    3 posts
    Member since:
    Oct 2013

    Posted 05 Mar 2014 in reply to Princy Link to this post

    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
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 06 Mar 2014 in reply to Rajeev Link to this post

    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
  6. Sushilkumar
    Sushilkumar avatar
    1 posts
    Member since:
    Jun 2012

    Posted 20 Jan in reply to Princy Link to this post

    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();
            }

  7. chinnu Blessen
    chinnu Blessen avatar
    1 posts
    Member since:
    Feb 2016

    Posted 24 Feb Link to this post

    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>

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017