这就是我说的需要递归实现。
对上面做两处改动,
1 用DataChanged事件
- <script lang="javascript" type="text/javascript">
- window.onload = function () {
- var spread1 = document.getElementById("<%=FpSpread1.ClientID %>");
- if (document.all) {
- // IE
- if (spread1.addEventListener) {
- // IE9
- spread1.addEventListener("DataChanged", cellChanged, false);
- } else {
- // Other versions of IE and IE9 quirks mode (no doctype set)
- spread1.onActiveCellChanged = cellChanged;
- }
- }
- else {
- // Firefox
- spread1.addEventListener("DataChanged", cellChanged, false);
- }
- function cellChanged(event) {
- spread1.Update();
- }
- }
-
- </script>
复制代码
2. 递归计算total
- private void FpSpread1_ChildViewCreated(object sender, CreateChildViewEventArgs e)
- {
- e.SheetView.ColumnHeader.Visible = false;
- e.SheetView.Columns[0].Width = 48;
- if (e.SheetView.RelationName == "Second")
- {
- e.SheetView.Columns[0].Width = 6;
- }
- for (int i = 0; i < e.SheetView.RowCount; i++)
- {
- e.SheetView.Cells[i, 4].Formula = "D" + (i + 1).ToString() + "*10";
- }
- e.SheetView.AutoCalculation = true;
- ReCalc(e.SheetView);
- }
- private void ReCalc(SheetView sheetview)
- {
- if (sheetview == null || sheetview.Parent == null)
- {
- return;
- }
- int total = 0;
- for (int i = 0; i < sheetview.RowCount; i++)
- {
- total += (int)sheetview.Cells[i, 3].Value;
- }
- sheetview.Parent.Cells[sheetview.ParentRowIndex, 3].Value = total;
- ReCalc(sheetview.Parent);
- }
复制代码 |