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

Need Previous row calculated data after I click Add New Record

4 Answers 118 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Sukhbinder
Top achievements
Rank 1
Sukhbinder asked on 21 Jun 2012, 07:02 AM
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

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 21 Jun 2012, 07:44 AM
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.
0
Sukhbinder
Top achievements
Rank 1
answered on 21 Jun 2012, 11:21 AM
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.

0
Shinu
Top achievements
Rank 2
answered on 22 Jun 2012, 03:40 AM
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.
0
Sukhbinder
Top achievements
Rank 1
answered on 22 Jun 2012, 04:16 AM

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

Tags
General Discussions
Asked by
Sukhbinder
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Sukhbinder
Top achievements
Rank 1
Share this question
or