Need Previous row calculated data after I click Add New Record

5 posts, 0 answers
  1. Sukhbinder
    Sukhbinder avatar
    7 posts
    Member since:
    May 2012

    Posted 21 Jun 2012 Link to this post

    Hello Telerik Team,

    Being new to this tool, I hava requirement where by I have a Rad Grid and when I submit / click Add new Record button the text boxes to be filled automatically after some calculated values from the previous rows.

    Please help in this this issues as ItemCommand is not allowing me to have access to the text boxes.

    Regards
    SS Bhogal
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 21 Jun 2012 Link to this post

    Hi,

    I am not quite sure about your requirement.Please take a look into the following sample code snippet to access the textox in ItemCommand Event while adding new record.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" onitemcommand="RadGrid1_ItemCommand" onneeddatasource="RadGrid1_NeedDataSource">
        <MasterTableView CommandItemDisplay="Top">
            <Columns>
                <telerik:GridBoundColumn UniqueName="OrderID" HeaderText="OrderID" DataField="OrderID"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="CustomerID" HeaderText="CustomerID" DataField="CustomerID"></telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    C#:
    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.InitInsertCommandName)
        {
            e.Canceled = true;
            e.Item.OwnerTableView.InsertItem();
            GridEditableItem insertedItem = e.Item.OwnerTableView.GetInsertItem();
            GridEditFormItem editFormItem = insertedItem as GridEditFormItem;
            TextBox OrderID = (TextBox)editFormItem["OrderID"].Controls[0];
            OrderID.Text = "your text";
        }
    }
    Please elaborate your scenario if it doesn't help.

    Thanks,
    Shinu.
  3. Sukhbinder
    Sukhbinder avatar
    7 posts
    Member since:
    May 2012

    Posted 21 Jun 2012 Link to this post

    Thank you Shinu,

    But it did not work. The thing is I am doing is -

    <telerik:GridTemplateColumn HeaderText="Amount From" AllowFiltering="false"

    HeaderStyle-Width="15%">

    <ItemTemplate>

    <asp:Label ID="lbl_TxnAmtFrom" runat="server" Text='<%#Eval("TransactionAmountFrom")%>'></asp:Label>

    </ItemTemplate>

    <EditItemTemplate>

    <span>

    <asp:TextBox runat="server" ID="txt_TxnAmtFrom" Width="150px" Text='<%# Bind("TransactionAmountFrom") %>'>

    </asp:TextBox><span style="color: Red">

    </span>

    </EditItemTemplate>

    <HeaderStyle Width="15%"></HeaderStyle>

    </telerik:GridTemplateColumn>


    I attached the picture I need.

  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 21 Jun 2012 Link to this post

    Hi Sukhbinder,

    Access the Label 'AmountTo' in ItemDataBount event and store the value of the Label in a global variable and do the calculation, then access the TextBox 'AmountFrom' in the ItemCommand event while adding new record and assign the global variable value in the TextBox. Please take a look into the following code snippet.

    C#:
    public static int value = 0;
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
      if (e.Item is GridDataItem)
      {
         GridDataItem ditem = (GridDataItem)e.Item;
         Label OrderID = (Label)ditem.FindControl("lbl_TxnAmtTo");
         value=Convert.ToInt32(OrderID.Text);
         value = value + 1;
      }
    }
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.InitInsertCommandName)
        {
            e.Canceled = true;
            e.Item.OwnerTableView.InsertItem();
            GridEditableItem insertedItem = e.Item.OwnerTableView.GetInsertItem();
            GridEditFormItem editFormItem = insertedItem as GridEditFormItem;
            TextBox OrderID = (TextBox)editFormItem.FindControl("txt_TxnAmtFrom");
            OrderID.Text = value.ToString();
        }
    }

    Thanks,
    Shinu.
  5. Sukhbinder
    Sukhbinder avatar
    7 posts
    Member since:
    May 2012

    Posted 21 Jun 2012 Link to this post

    Thanks Shinu,

    I could do it yesterday by getting the control in the RadGrid as - 

    e.Canceled = true;
    e.Item.OwnerTableView.InsertItem();
    GridEditableItem insertedItem = e.Item.OwnerTableView.GetInsertItem();
    TextBox box = insertedItem.FindControl("txt_TxnAmtFrom") as TextBox;

    It could solve my purpose and of-course thank you for the efforts you did.

    Regards

    SS Bhogal

Back to Top