如何在C#wpf中进行行计算?

如何在C#wpf中进行行计算?,c#,C#,在下图中,每行的期初余额为: 如果为贷方,则为上述列的总和 若为借方,则为上述列的负数 用c#怎么做 例如: credit debit OpenBal 1. 100 - 0 - 100 2. 90 - 0 - 190 3. 100 - 0 - 290 4. 0 - 50 - 240 5. 0 - 100 - 140 6. 150 -

在下图中,每行的期初余额为:

  • 如果为贷方,则为上述列的总和
  • 若为借方,则为上述列的负数
用c#怎么做

例如:

    credit    debit    OpenBal
 1.   100  -     0   -   100
 2.    90  -     0   -   190
 3.   100  -     0   -   290
 4.     0  -    50   -   240
 5.     0  -   100   -   140
 6.   150  -     0   -   290

根据计算值的时间/方式/地点,有多种可能性

以下示例演示了如何在一个非常简单的情况下实现计算,即在加载数据后直接计算OpenBalance,而不必刷新OpenBalance:

“加载”数据: 计算未结余额:
for(int i=0;i
您需要给我们一些可以使用的东西(代码!)。让我们看看你的努力。我们不会为您编写程序。
var tbl = new DataTable();
tbl.Columns.Add( "credit", typeof( decimal ) );
tbl.Columns.Add( "debit", typeof( decimal ) );
tbl.Columns.Add( "OpenBal", typeof( decimal ) );

tbl.Rows.Add( 100, 0 );
tbl.Rows.Add( 90, 0 );
tbl.Rows.Add( 100, 0 );
tbl.Rows.Add( 0, 50 );
tbl.Rows.Add( 0, 100 );
tbl.Rows.Add( 150, 0 );
for ( int i = 0; i < tbl.Rows.Count; i++ )
{
    var row = tbl.Rows[i];
    if ( i == 0 )
        row["OpenBal"] = (decimal)row["credit"] - (decimal)row["debit"];
    else
    {
        var previousRow = tbl.Rows[i-1];
        row["OpenBal"] = (decimal)previousRow["OpenBal"] + ( decimal)row["credit"] - (decimal)row["debit"];
    }
}