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.