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

Editing the Radgrid in Batch Edit Mode when autogenerated columns true finding the sum of all the cells in row selected event

5 Answers 354 Views
Grid
This is a migrated thread and some comments may be shown as answers.
shareen
Top achievements
Rank 1
shareen asked on 05 Jun 2015, 11:29 AM

We have a rad grid.We are binding the grid using datatable and auto generated columns are true.

We want to add all the cell values and display the sum of all the cell values in other column when cell click.

we have tried with row selected event but the sum is not displaying properly when moving from one cell to other.

Please help us to fix the issue.

 

1.       We are
able to fetch the selected cell value but unable to fetch the remaining
corresponding cell values. You can find the same in the sample project
(BatchEdit.aspx – OnBatchEditCellValueChanged event)

 2.       We are
able to fetch the selected cell value and corresponding cell values in the same
row by hardcoding the column names, but dynamically we are unable to get the
values of the cell. You can find the same in the sample project
(OnRowSelected.aspx – OnRowSelected event)

we have used the following code

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>

  <script type="text/javascript">
      function RowSelected(sender, eventArgs) {
          var grid = sender;
          var MasterTable = grid.get_masterTableView();
         
          var row = MasterTable.get_dataItems()[eventArgs.get_itemIndexHierarchical()];
          var cell1 = MasterTable.getCellByColumnUniqueName(row, "Value1");
          var cell2 = MasterTable.getCellByColumnUniqueName(row, "Value2");
          var cell3 = MasterTable.getCellByColumnUniqueName(row, "Value3");
          var cell4 = MasterTable.getCellByColumnUniqueName(row, "Value4");
          var total = 0;
          var arr = [];
          var total = 0;
      
          var columnlength = grid.get_masterTableView().get_columns().length;
          for (i = 0 ; i < columnlength; i++) {
              cell = grid.get_masterTableView().get_columns()[i].get_uniqueName();
           
              var cell = MasterTable.getCellByColumnUniqueName(row, cell);
              var val = parseInt(cell.innerText);
              arr[i] = val;
             

          }
          for (i = 1; i < arr.length; i++) {

              total += arr[i];
          }

          var cell1 = MasterTable.getCellByColumnUniqueName(row, "Sum");
          cell1.innerText = total;
          cell1.eventArgs.set_cancel(true);


      }
    </script>
   <%-- var grid = $find("<%=RadGrid1.ClientID%>");
    var tableView = sender.get_masterTableView();
    var batchManager = sender.get_batchEditingManager();
    var items = tableView.get_dataItems();
    var mapValues = [];
    for (var i = 0; i < items.length; i++) {
        var mapCell = items[i].get_cell("Map");
        var mapValue = batchManager.getCellValue(mapCell);
        mapValues.push(mapValue);
    }--%>
</head>
<body>
    <form id="form1" runat="server">
         <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
         <asp:HiddenField ID="HiddenField1" runat="server" />
    <div>
    <telerik:RadGrid ID="RadGrid1" runat="server">
        <MasterTableView EditMode="Batch" BatchEditingSettings-EditType="Cell" >
        </MasterTableView>
        <ClientSettings>
            <Selecting AllowRowSelect="True" CellSelectionMode="MultiCell" />
            <ClientEvents OnRowClick="RowSelected" />
        </ClientSettings>
    </telerik:RadGrid>
    </div>
    </form>
</body>
</html>

and code behind is

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class OnRowSelected : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GetValues();
    }
    private void GetValues()
    {
        DataTable dtTable = new DataTable();
        dtTable.Columns.Add("Sum");
        dtTable.Columns.Add("Value1"); dtTable.Columns.Add("Value2");
        dtTable.Columns.Add("Value3"); dtTable.Columns.Add("Value4");

        DataRow dr = dtTable.NewRow();
        dr["Sum"] = "";
        dr["Value1"] = "1"; dr["Value2"] = "2";
        dr["Value3"] = "3"; dr["Value4"] = "4";
        dtTable.Rows.Add(dr);

        DataRow drr = dtTable.NewRow();
        drr["Sum"] = "";
        drr["Value1"] = "5"; drr["Value2"] = "6";
        drr["Value3"] = "7"; drr["Value4"] = "8";
        dtTable.Rows.Add(drr);


        RadGrid1.DataSource = dtTable;
        RadGrid1.DataBind();

    }
}

Any other information please contact us.

5 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 10 Jun 2015, 07:45 AM
Hi Shareen,

If you would like to calculate the value in a cell immediately after the user makes a change in one of the cells you can use the OnBatchEditClosed event. In the handler you can iterate through the columns in RadGrid and perform the necessary calculations.

Check out the following code snippet that illustrates the approach.

function batchEditClosed(sender, args) {
    var grid = sender;
    var masterTableView = grid.get_masterTableView();
    var batchEditManager = grid.get_batchEditingManager();
 
    var rowIndex = args.get_row().id.split("__")[1];
    var dataItem = masterTableView.get_dataItems()[rowIndex];
    var currentValue;
    var total = 0 ;
 
    for (var i = 0; i < masterTableView.get_columns().length; i++) {
        currentValue = batchEditManager.getCellValue(dataItem.get_cell(masterTableView.get_columns()[i].get_uniqueName()));
 
        total += currentValue;
 
    }
 
}


On a side note, in the code you provided DataBind() is used to bind RadGrid. Have in mind that this method is used for simple data binding. It is suitable only for the most simple scenarios.

It is recommended to use advanced data binding. You can either handle the NeedDataSource event or use a declarative DataSource control to provide data for RadGrid.


Regards,
Viktor Tachev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Supriya
Top achievements
Rank 1
answered on 17 Aug 2015, 05:00 AM
Hi, 

In our project we are binding radgrid using dynamic columns(Autogenerate columns). This dynamic columns are not fixed in length also it contains template columns like dropdownlist,textbox and datetime picker. Our requirement is to edit cell values and then to update this changes in sql database table. But as this columns are dynamic we don't know column name in RadGrid1_BatchEditCommand, RadGrid1_ItemUpdated and RadGrid1_ItemInserted how to perform grid "Batch editing" and "Add new record" operation for autogenerate columns. Please provide any sample application
0
Viktor Tachev
Telerik team
answered on 18 Aug 2015, 12:46 PM
Hi Supriya,

When using autogenerated columns for RadGrid the UniqueName for every column is based on the DataField it is bound to. You can use this when updating the data in the data source.

Moreover, you could use automatic CRUD operations with RadGrid. The functionality is described in more detail in this article.

Regards,
Viktor Tachev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Supriya
Top achievements
Rank 1
answered on 18 Aug 2015, 01:01 PM

Hi Viktor,

Thanks for reply.

Could you please provide sample application for same? 

 

0
Viktor Tachev
Telerik team
answered on 20 Aug 2015, 01:34 PM
Hi,

I am attaching a sample project that illustrates RadGrid with autogenerated columns and Batch Editing enabled. The sample illustrates how you can update the data and insert new records.

There is dummy data used in the sample, however, in a real world scenario the approach for updating the data in the datasource would be similar.

Regards,
Viktor Tachev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
shareen
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Supriya
Top achievements
Rank 1
Share this question
or