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

ColumnCalculated

5 Answers 184 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Hugo
Top achievements
Rank 1
Hugo asked on 16 Mar 2012, 01:27 AM
I've two RadNumericTextBox with records, how i can sum the records and place them  in GridCalculatedColumn?

<telerik:GridTemplateColumn headerText="Importe" UniqueName="fcimporte" HeaderStyle-HorizontalAlign="Center">
                                            <ItemTemplate>
                                                <telerik:RadNumericTextBox runat="server" Width="50px" MaxLength="5" DbValue='<%# Eval("fcimporte") %>' Type="Currency">
                                                <ClientEvents OnBlur="Blur" OnFocus="Focus" />
                                                </telerik:RadNumericTextBox>
                                            </ItemTemplate>
                                            <ItemStyle HorizontalAlign="Center" />
                                         </telerik:GridTemplateColumn>
 
                                         <telerik:GridTemplateColumn HeaderText="IVA ($)" UniqueName="fciva" HeaderStyle-HorizontalAlign="Center">
                                            <ItemTemplate>
                                                <telerik:RadNumericTextBox runat="server" Width="50px" MaxLength="5" DbValue='<%# Eval("fciva") %>' Type="Currency">
                                                </telerik:RadNumericTextBox>
                                            </ItemTemplate>
                                         </telerik:GridTemplateColumn>

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 16 Mar 2012, 07:14 AM
Hello Hugo,

GridCalculatedColumn displays a value that is calculated based on one or more fields and an expression that indicates how to calculate the display value. Use the DataFields property to list all the fields that are used to calculate the column value. The Expression property then specifies how the field values are to be combined, using parameters based on the order of the fields listed in the DataFields property:
The following code snippet shows how to achieve this.
aspx:
<telerik:GridCalculatedColumn UniqueName="Name" HeaderText="Calculated Column" DataFields="fcimporte, fciva" Expression='{0} + {1}' >
</telerik:GridCalculatedColumn>

Thanks,
Shinu.
0
Hugo
Top achievements
Rank 1
answered on 17 Mar 2012, 12:43 AM
Hi Shinu!!!!
I understand the details thanks so much!!!, 

Another question,
do you have any idea of how to calculated this column, automatically, while changing the amount?


0
Shinu
Top achievements
Rank 2
answered on 19 Mar 2012, 09:09 AM
Hello Hugo,

In order to achieve this you can attach 'OnFocus' and 'OnBlur' event to RadNumericTextBox and can do the calculation from javascript like below. I am attaching 'OnFocus' client event from code behind to get the corresponding item index.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" AllowMultiRowSelection="true"
           DataSourceID="SqlDataSource1" OnItemDataBound="RadGrid1_ItemDataBound">
           <MasterTableView CommandItemDisplay="Top" >
               <Columns>
                   <telerik:GridTemplateColumn HeaderText="Importe" UniqueName="fcimporte" HeaderStyle-HorizontalAlign="Center">
                       <ItemTemplate>
                           <telerik:RadNumericTextBox ID="RadNumericTextBox1" runat="server" Width="50px" MaxLength="5"
                               DbValue='<%# Eval("fcimporte") %>' Type="Currency">
                               <ClientEvents OnBlur="RadNumericTextBox1Blur" />
                           </telerik:RadNumericTextBox>
                       </ItemTemplate>
                       <ItemStyle HorizontalAlign="Center" />
                   </telerik:GridTemplateColumn>
                   <telerik:GridTemplateColumn HeaderText="IVA ($)" UniqueName="fciva" HeaderStyle-HorizontalAlign="Center">
                       <ItemTemplate>
                           <telerik:RadNumericTextBox ID="RadNumericTextBox2" runat="server" Width="50px" MaxLength="5"
                               DbValue='<%# Eval("fciva") %>' Type="Currency">
                                <ClientEvents OnBlur="RadNumericTextBox2Blur" />
                           </telerik:RadNumericTextBox>
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
                   <telerik:GridCalculatedColumn UniqueName="Name" HeaderText="Calculated Column" DataFields="fcimporte, fciva"
                       Expression='{0} + {1}'>
                   </telerik:GridCalculatedColumn>
                 </Columns>
           </MasterTableView>
           <ClientSettings Selecting-AllowRowSelect="true">
           </ClientSettings>
       </telerik:RadGrid>

JavaScript:
<script type="text/javascript">
    var tempValue = 0.0;
    var itemIndex;
 
    function RadNumericTextBox1Blur(sender, args) {
        var grid = $find("<%=RadGrid1.ClientID %>");
        var MasterTable = grid.get_masterTableView();
        var row = MasterTable.get_dataItems()[itemIndex];
        row.get_cell("Name").innerText = parseFloat(tempValue) + parseFloat(sender.get_value());
    }
    function RadNumericTextBox1Focus(sender, roxIndex) {
        var grid = $find("<%=RadGrid1.ClientID %>");
        var MasterTable = grid.get_masterTableView();
        itemIndex = roxIndex;
        var row = MasterTable.get_dataItems()[roxIndex];
       tempValue = row.findControl("RadNumericTextBox2").get_value();
   }
   function RadNumericTextBox2Blur(sender, args) {
       var grid = $find("<%=RadGrid1.ClientID %>");
       var MasterTable = grid.get_masterTableView();
       var row = MasterTable.get_dataItems()[itemIndex];
       row.get_cell("Name").innerText = parseFloat(tempValue) + parseFloat(sender.get_value());
   }
   function RadNumericTextBox2Focus(sender, roxIndex) {
       var grid = $find("<%=RadGrid1.ClientID %>");
       var MasterTable = grid.get_masterTableView();
       itemIndex = roxIndex;
       var row = MasterTable.get_dataItems()[roxIndex];
       tempValue = row.findControl("RadNumericTextBox1").get_value();
   }
</script>

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
  {
      if ((e.Item is GridDataItem))
      {
          GridDataItem item = (GridDataItem)e.Item;
          RadNumericTextBox txtbox1 = (RadNumericTextBox)item.FindControl("RadNumericTextBox1");
          txtbox1.Attributes.Add("OnFocus", "RadNumericTextBox1Focus(this,'" + item.ItemIndex + "');");
          RadNumericTextBox txtbox2 = (RadNumericTextBox)item.FindControl("RadNumericTextBox2");
          txtbox2.Attributes.Add("OnFocus", "RadNumericTextBox2Focus(this,'" + item.ItemIndex + "');");
      }
  }

Thanks,
Shinu.



0
Hugo
Top achievements
Rank 1
answered on 27 Mar 2012, 01:02 AM
Thank so much Shinu this code is perfect for my project now to finish!!!

0
Hugo
Top achievements
Rank 1
answered on 28 Mar 2012, 12:33 AM
Hi Shinu!!!

I have serious problems!!!!

My code is not calculating, the result is 10016 when i need at sum 116.00. You know the problem.


Tags
Grid
Asked by
Hugo
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Hugo
Top achievements
Rank 1
Share this question
or