You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SAP Business One矩阵控件列合计不显示问题求助(仅末列生效)

SAP Business One 10.00.150 矩阵控件合计列失效问题

初始问题

在SAP Business One v.10.00.150的插件中实现了带矩阵控件的自定义窗体,矩阵已绑定DataTable且其他功能正常,但部分矩阵列无法显示合计值——合计行已生成,但值始终为空。实现代码如下:

static Column AddCol( Cols cols, string id, string title, BoFieldsType type, BoFormItemTypes itype, bool editable )
{   
    Column mcol;
    // cols.Mx 是 SAPbouiCOM.Columns
    mcol = cols.Mx.Add( id, itype );
    mcol.TitleObject.Caption = title;
    cols.Dt.Add( id, type, 254 );
    mcol.DataBind.Bind( DtId, id );
    mcol.Editable = editable;
    if( type == ft_Quantity )
    {   
        mcol.ColumnSetting.SumType = BoColumnSumType.bst_Auto;  
    }
    return mcol;
}

问题定位更新

现已发现核心问题:自动合计仅对矩阵的最后一列生效。当在设置了自动合计的列后添加其他列时,之前列的合计会意外失效。测试代码如下:

// dt 是 SAPbouiCOM.DataTable
// mx 是 SAPbouiCOM.Matrix
Column   col;
dt.Columns.Add( "1", BoFieldsType.ft_Quantity, 0 );
col = mx.Columns.Add( "1", BoFormItemTypes.it_EDIT );
col.TitleObject.Caption = "应该显示合计";
col.DataBind.Bind( dt.UniqueID, "1" );
col.ColumnSetting.SumType = BoColumnSumType.bst_Auto;

// 若在带自动合计的列后添加其他列,自动合计会失效——可尝试切换true/false验证:
if( true )
{   
    dt.Columns.Add( "2", BoFieldsType.ft_AlphaNumeric, 254 );
    col = mx.Columns.Add( "2", BoFormItemTypes.it_EDIT );
    col.TitleObject.Caption = "会导致合计失效";
    col.DataBind.Bind( dt.UniqueID, "2" );
}

dt.Rows.Add(2);
dt.SetValue( "1", 0, 1.00 );
dt.SetValue( "1", 1, 0.23 );
mx.LoadFromDataSource();

解决方法

这是SAP Business One矩阵控件的已知行为:默认的自动合计仅支持最后一列。要让非末列也能正常显示合计,需通过手动计算赋值的方式实现,具体步骤:

  • 开启矩阵合计行:确保矩阵的AutoSum属性设为true,同时TotalRow属性设为true(若未默认开启)。
  • 手动计算合计值:在数据加载完成后(比如执行mx.LoadFromDataSource()之后,或在AfterLoadFromDataSource事件中),遍历目标列的所有数据行计算总和,再赋值给矩阵合计行的对应列。
  • 实时更新合计:若矩阵数据支持编辑,需在AfterLostFocus或AfterDataChange事件中重新计算合计,保证数据变更后合计值同步更新。

手动计算合计的示例代码:

// 计算指定列的合计并赋值到合计行
private void CalculateMatrixTotal(SAPbouiCOM.Matrix mx, SAPbouiCOM.DataTable dt, string colId)
{
    double total = 0;
    // 遍历所有数据行累加数值
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        if (dt.GetValue(colId, i) != null && double.TryParse(dt.GetValue(colId, i).ToString(), out double val))
        {
            total += val;
        }
    }
    // 赋值给矩阵的合计行(合计行索引为总行数+1)
    mx.Columns.Item(colId).Cells.Item(mx.RowCount + 1).Specific.Value = total.ToString("0.00");
}

// 调用时机:执行mx.LoadFromDataSource()之后
CalculateMatrixTotal(mx, dt, "1");

若布局允许,也可以调整列的添加顺序,将需要合计的列放在矩阵最后,这样即可使用默认的自动合计功能。

内容的提问来源于stack exchange,提问作者Anton Shepelev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 17:04:58