|
1 | | -<!-- default badges list --> |
2 | | - |
3 | | -[](https://supportcenter.devexpress.com/ticket/details/T517531) |
4 | | -[](https://docs.devexpress.com/GeneralInformation/403183) |
5 | | -<!-- default badges end --> |
6 | | -<!-- default file list --> |
7 | | -*Files to look at*: |
8 | | - |
9 | | -* [Default.aspx](./CS/Default.aspx) (VB: [Default.aspx](./VB/Default.aspx)) |
10 | | -* [Default.aspx.cs](./CS/Default.aspx.cs) (VB: [Default.aspx.vb](./VB/Default.aspx.vb)) |
11 | | -<!-- default file list end --> |
12 | | -# ASPxGridView - Batch Editing - How to update summaries when HighlightDeletedRows=true |
| 1 | +# Grid View for ASP.NET Web Forms - How to update total summaries on the client in batch edit mode when deleted rows are highlighted |
13 | 2 | <!-- run online --> |
14 | 3 | **[[Run Online]](https://codecentral.devexpress.com/t517531/)** |
15 | 4 | <!-- run online end --> |
16 | 5 |
|
| 6 | +This example demonstrates how to calculate the total summary dynamically in batch edit mode when the grid's [HighlightDeletedRows](https://docs.devexpress.com/AspNet/DevExpress.Web.GridViewBatchEditSettings.HighlightDeletedRows) property is enabled. |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +### Create a Custom Summary Item |
| 13 | + |
| 14 | +Add a total summary item for the corresponding column. Use the item's [Tag](https://docs.devexpress.com/AspNet/DevExpress.Web.ASPxSummaryItemBase.Tag) property to identify the summary item and get its value. |
| 15 | + |
| 16 | +```aspx |
| 17 | +<TotalSummary> |
| 18 | + <dx:ASPxSummaryItem SummaryType="Sum" FieldName="C2" Tag="C2_Sum" /> |
| 19 | +</TotalSummary> |
| 20 | +``` |
17 | 21 |
|
18 | | -<p>This example illustrates how to modify the <a href="https://www.devexpress.com/Support/Center/p/T114923">ASPxGridView - How to update total summaries on the client side in Batch Edit mode</a> example to correctly calculate the total summary if the <a href="https://documentation.devexpress.com/#AspNet/DevExpressWebGridViewBatchEditSettings_HighlightDeletedRowstopic">GridViewBatchEditSettings.HighlightDeletedRows</a> property is enabled. <br><br></p> |
19 | | -<p><strong>See Also:</strong></p> |
20 | | -<p><a href="https://www.devexpress.com/Support/Center/p/T114539">ASPxGridView - Batch Edit - How to calculate values on the fly</a> <br><a href="https://www.devexpress.com/Support/Center/p/T116925">ASPxGridView - Batch Edit - How to calculate unbound column and total summary values on the fly</a> <br><br><strong>ASP.NET MVC Example:</strong><br><a href="https://www.devexpress.com/Support/Center/p/T137186">GridView - How to update total summaries on the client side in Batch Edit mode</a></p> |
21 | | - |
22 | | - |
23 | | -<h3>Description</h3> |
24 | | - |
25 | | -<p>For this, create a custom Recovery button:</p> |
26 | | -<code lang="aspx"><dx:GridViewCommandColumn ShowNewButtonInHeader="true" ShowDeleteButton="true"> |
27 | | - <CustomButtons> |
28 | | - <dx:GridViewCommandColumnCustomButton ID="customRecover" Text="Recover"></dx:GridViewCommandColumnCustomButton> |
29 | | - </CustomButtons> |
30 | | -</dx:GridViewCommandColumn> |
31 | | -</code> |
32 | | -<p> To manipulate the button visibility, add the following custom CSS classes:</p> |
33 | | -<code lang="aspx"><Styles> |
34 | | - <CommandColumnItem CssClass="commandCell"></CommandColumnItem> |
35 | | - <BatchEditDeletedRow CssClass="deletedRow"></BatchEditDeletedRow> |
36 | | -</Styles> |
37 | | -</code> |
38 | | -<p> These CSS rules allow showing a custom Recovery button when it is required:</p> |
39 | | -<code lang="css">.deletedRow a[data-args*="customRecover"].commandCell { |
| 22 | +```cs |
| 23 | +protected object GetTotalSummaryValue() { |
| 24 | + ASPxSummaryItem summaryItem = Grid.TotalSummary.First(i => i.Tag == "C2_Sum"); |
| 25 | + return Grid.GetTotalSummaryValue(summaryItem); |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +Replace the summary item with a custom [footer template](https://docs.devexpress.com/AspNet/DevExpress.Web.GridViewColumn.FooterTemplate). |
| 30 | + |
| 31 | +```aspx |
| 32 | +<dx:GridViewDataSpinEditColumn Width="100" FieldName="C2"> |
| 33 | + <FooterTemplate> |
| 34 | + Sum = |
| 35 | + <dx:ASPxLabel ID="ASPxLabel1" runat="server" ClientInstanceName="labelSum" Text='<%# GetTotalSummaryValue() %>' /> |
| 36 | + </FooterTemplate> |
| 37 | +</dx:GridViewDataSpinEditColumn> |
| 38 | +``` |
| 39 | + |
| 40 | +Handle the grid's client-side [BatchEditEndEditing](https://docs.devexpress.com/AspNet/js-ASPxClientGridView.BatchEditEndEditing) and [BatchEditRowDeleting](https://docs.devexpress.com/AspNet/js-ASPxClientGridView.BatchEditRowDeleting) events. In handlers, use the grid's [batchEditApi.GetCellValue](https://docs.devexpress.com/AspNet/js-ASPxClientGridViewBatchEditApi.GetCellValue(visibleIndex-columnFieldNameOrId)) method to get initial cell values and `rowValues` argument property to get new cell values. Then recalculate the summary value and assign it to the label. |
| 41 | + |
| 42 | +```js |
| 43 | +function OnBatchEditEndEditing(s, e) { |
| 44 | + CalculateSummary(s, e.rowValues, e.visibleIndex, false); |
| 45 | +} |
| 46 | +function CalculateSummary(grid, rowValues, visibleIndex, isDeleting) { |
| 47 | + var originalValue = grid.batchEditApi.GetCellValue(visibleIndex, "C2"); |
| 48 | + var newValue = rowValues[(grid.GetColumnByField("C2").index)].value; |
| 49 | + var dif = isDeleting ? -newValue : newValue - originalValue; |
| 50 | + labelSum.SetValue((parseFloat(labelSum.GetValue()) + dif).toFixed(1)); |
| 51 | +} |
| 52 | +function OnBatchEditRowDeleting(s, e) { |
| 53 | + CalculateSummary(s, e.rowValues, e.visibleIndex, true); |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +### Create a Custom Recovery Button |
| 58 | + |
| 59 | +Use the command column's [CustomButtons](https://docs.devexpress.com/AspNet/DevExpress.Web.GridViewCommandColumn.CustomButtons) property to create a custom **Recovery** button. |
| 60 | + |
| 61 | +```aspx |
| 62 | +<dx:GridViewCommandColumn ShowNewButtonInHeader="true" ShowDeleteButton="true" ShowRecoverButton="true"> |
| 63 | + <CustomButtons> |
| 64 | + <dx:GridViewCommandColumnCustomButton ID="customRecover" Text="Recover"></dx:GridViewCommandColumnCustomButton> |
| 65 | + </CustomButtons> |
| 66 | +</dx:GridViewCommandColumn> |
| 67 | +``` |
| 68 | + |
| 69 | +Add custom CSS classes to control the custom button's visibility based on a condition and hide the default **Recovery** button. |
| 70 | + |
| 71 | +```aspx |
| 72 | +<Styles> |
| 73 | + <CommandColumnItem CssClass="commandCell"></CommandColumnItem> |
| 74 | + <BatchEditDeletedRow CssClass="deletedRow"></BatchEditDeletedRow> |
| 75 | +</Styles> |
| 76 | +``` |
| 77 | + |
| 78 | +```css |
| 79 | +.deletedRow a[data-args*="customRecover"].commandCell { |
40 | 80 | display: inline !important; |
41 | 81 | } |
| 82 | + |
42 | 83 | a[data-args*="customRecover"].commandCell { |
43 | 84 | display: none; |
44 | 85 | } |
45 | | -</code> |
46 | | -<p> To hide the standard Recovery button, use the following rule:</p> |
47 | | -<code lang="css">a[data-args*="Recover"].commandCell { |
| 86 | + |
| 87 | +a[data-args*="Recover"].commandCell { |
48 | 88 | display: none; |
49 | 89 | } |
50 | | -</code> |
51 | | -<p> Finally, handle the <a href="https://documentation.devexpress.com/#AspNet/DevExpressWebScriptsASPxClientGridView_CustomButtonClicktopic">ASPxClientGridView.CustomButtonClick</a> event to restore the row and calculate the total summary after that: </p> |
52 | | -<code lang="js">function OnCustomButtonClick(s, e) { |
| 90 | +``` |
| 91 | + |
| 92 | +Handle the grid's client-side [CustomButtonClick](https://docs.devexpress.com/AspNet/js-ASPxClientGridView.CustomButtonClick) event to restore the deleted row and recalculate the total summary. |
| 93 | + |
| 94 | +```js |
| 95 | +function OnCustomButtonClick(s, e) { |
53 | 96 | if (e.buttonID == 'customRecover') { |
54 | 97 | s.batchEditApi.ResetChanges(e.visibleIndex); |
55 | 98 | var value = s.batchEditApi.GetCellValue(e.visibleIndex, "C2"); |
56 | 99 | labelSum.SetValue((parseFloat(labelSum.GetValue()) + value).toFixed(1)); |
57 | 100 | } |
58 | 101 | } |
59 | | -</code> |
60 | | -<p> </p> |
| 102 | +``` |
| 103 | + |
| 104 | +## Files to Review |
| 105 | + |
| 106 | +* [Default.aspx](./CS/Default.aspx) (VB: [Default.aspx](./VB/Default.aspx)) |
| 107 | +* [Default.aspx.cs](./CS/Default.aspx.cs) (VB: [Default.aspx.vb](./VB/Default.aspx.vb)) |
| 108 | + |
| 109 | +## Documentation |
61 | 110 |
|
62 | | -<br/> |
| 111 | +* [Grid in Batch Edit Mode](https://docs.devexpress.com/AspNet/16443/components/grid-view/concepts/edit-data/batch-edit-mode) |
63 | 112 |
|
| 113 | +## More Examples |
64 | 114 |
|
| 115 | +* [Grid View for ASP.NET Web Forms - How to update total summaries on the client in batch edit mode](https://github.com/DevExpress-Examples/asp-net-web-forms-grid-update-total-summaries-on-client-in-batch-mode) |
| 116 | +* [Grid View for ASP.NET Web Forms - How to calculate values dynamically in batch edit mode](https://github.com/DevExpress-Examples/asp-net-web-forms-gridview-calculate-values-dynamically-batch-mode) |
| 117 | +* [Grid View for ASP.NET Web Forms - How to calculate values and update total summaries dynamically in batch edit mode](https://github.com/DevExpress-Examples/asp-net-web-forms-grid-calculate-column-values-and-total-summaries-in-batch-mode) |
| 118 | +* [Grid View for ASP.NET MVC - How to update total summaries on the client in batch edit mode](https://github.com/DevExpress-Examples/asp-net-mvc-grid-update-total-summaries-on-client-in-batch-mode) |
0 commit comments