My RadGrid is in batch mode, and I want to update a 3rd column if the 1st or 2nd column are updated. Given my javascript and markup below, it "kind of" works, because when I update column 1 or 2 and tab off (to go to next field) it updates column 3, so that's good. But there are a couple of problems...
1) After changing column 1 or 2, when I tab off (or mouse click off) the original cell remains in edit mode and doesn't close.
2) Regardless if I change a value or not, when I hit the up or down arrow from any edittable cell, the focus moves to the row and cell above or below, as desired. However, the highlighted row selection doesn't change, but remains on the original line. This is needed as I have a javascript function (not included here but seen in the markup via OnRowSelected) that I need to call.
How do I get around these 2 problems?
3) Bonus question: Any way to get around all the data manipulation I'm doing to turn the string "1,000.0000" into number 1000 for my calculation? Seems I should be able to reference the RadNumericTextBox and simply call .get_value() but maybe that's not possible?
function
replaceString(str, oldValue, newValue) {
// The string.replace function replaces first instance; use reg exp to replace all.
var
r =
new
RegExp(oldValue,
"g"
);
// g = global, so changes them all.
return
str.replace(r, newValue);
}
function
setNumber(val) {
var
temp = replaceString(val,
','
,
''
);
// Number (and parseFloat) don't account for commas
return
Number(temp);
}
function
BatchEditCellValueChanged(sender, args) {
var
grd = sender;
var
masterTable = grd.get_masterTableView();
var
rows = masterTable.get_dataItems();
var
rowArgs = args.get_row();
var
rowIndex = rowArgs.sectionRowIndex;
var
row = rows[rowIndex];
var
batchManager = grd.get_batchEditingManager();
var
colName = args.get_columnUniqueName();
// Column that's been changed
switch
(colName) {
case
'PurchaseOrderDetailQuantity'
:
case
'UnitPrice'
:
var
qty = batchManager.getCellValue(row.get_cell(
'PurchaseOrderDetailQuantity'
));
var
unitPrice = batchManager.getCellValue(row.get_cell(
'UnitPrice'
));
var
result = setNumber(qty) * setNumber(unitPrice);
if
(result !== 0) {
// Never set the amount to zero.
batchManager.changeCellValue(row.get_cell(
'PurchaseOrderDetailAmount'
), result);
}
break
;
}
}
<
telerik:RadGrid
ID
=
"grdPODetails"
runat
=
"server"
AllowSorting
=
"True"
AutoGenerateColumns
=
"False"
Skin
=
"Office2010Blue"
GridLines
=
"None"
>
<
HeaderContextMenu
EnableAutoScroll
=
"True"
>
</
HeaderContextMenu
>
<
MasterTableView
CommandItemDisplay
=
"Top"
EditMode
=
"Batch"
InsertItemDisplay
=
"Bottom"
ClientDataKeyNames
=
"PurchaseOrderDetailKey"
DataKeyNames
=
"PurchaseOrderDetailKey"
>
<
BatchEditingSettings
EditType
=
"Cell"
OpenEditingEvent
=
"Click"
/>
<
CommandItemSettings
ShowRefreshButton
=
"False"
ShowSaveChangesButton
=
"true"
ShowCancelChangesButton
=
"true"
/>
<
NoRecordsTemplate
>
No detail lines to display.
</
NoRecordsTemplate
>
<
Columns
>
<
telerik:GridTemplateColumn
DataField
=
"PurchaseOrderDetailQuantity"
HeaderText
=
"Quantity"
UniqueName
=
"PurchaseOrderDetailQuantity"
DataType
=
"System.Double"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"lblPurchaseOrderDetailQuantity"
runat
=
"server"
Text='<%# Eval("PurchaseOrderDetailQuantity") %>'>
</
asp:Label
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
telerik:RadNumericTextBox
ID
=
"txtPurchaseOrderDetailQuantity"
runat
=
"server"
DataType
=
"System.Decimal"
MaxLength
=
"23"
Width
=
"100%"
IncrementSettings-InterceptArrowKeys
=
"false"
Culture
=
"English (United States)"
DbValue='<%# Bind("PurchaseOrderDetailQuantity") %>' Type="Number" NumberFormat-DecimalDigits="4" />
</
EditItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridTemplateColumn
DataField
=
"UnitPrice"
HeaderText
=
"UnitPrice"
UniqueName
=
"UnitPrice"
DataType
=
"System.Double"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"lblUnitPrice"
runat
=
"server"
Text='<%# Eval("UnitPrice") %>'>
</
asp:Label
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
telerik:RadNumericTextBox
ID
=
"txtUnitPrice"
runat
=
"server"
DataType
=
"System.Decimal"
MaxLength
=
"23"
Width
=
"100%"
IncrementSettings-InterceptArrowKeys
=
"false"
Culture
=
"English (United States)"
DbValue='<%# Bind("UnitPrice") %>' Type="Number" NumberFormat-DecimalDigits="4" />
</
EditItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridTemplateColumn
DataField
=
"PurchaseOrderDetailAmount"
HeaderText
=
"Amount"
UniqueName
=
"PurchaseOrderDetailAmount"
DataType
=
"System.Double"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"lblPurchaseOrderDetailAmount"
runat
=
"server"
Text='<%# Eval("PurchaseOrderDetailAmount") %>'>
</
asp:Label
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
telerik:RadNumericTextBox
ID
=
"txtPurchaseOrderDetailAmount"
runat
=
"server"
DataType
=
"System.Decimal"
MaxLength
=
"21"
Width
=
"100%"
IncrementSettings-InterceptArrowKeys
=
"false"
Culture
=
"English (United States)"
DbValue='<%# Bind("PurchaseOrderDetailAmount") %>' Type="Number" NumberFormat-DecimalDigits="2" />
</
EditItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridNumericColumn
DataField
=
"PurchaseOrderDetailKey"
HeaderText
=
"PurchaseOrderDetailKey"
UniqueName
=
"PurchaseOrderDetailKey"
DataType
=
"System.Int32"
Visible
=
"False"
ReadOnly
=
"True"
>
</
telerik:GridNumericColumn
>
</
Columns
>
</
MasterTableView
>
<
ClientSettings
AllowKeyboardNavigation
=
"true"
>
<
Selecting
AllowRowSelect
=
"True"
/>
<
ClientEvents
OnRowSelected
=
"RowSelected"
OnBatchEditCellValueChanged
=
"BatchEditCellValueChanged"
/>
</
ClientSettings
>
</
telerik:RadGrid
>