根据c#中数据表中的前一行计算行 [英] calculate row based on previous row in datatable in c#

查看:70
本文介绍了根据c#中数据表中的前一行计算行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有列的数据表:col1,col2,col3

假设dataTable具有以下内容



col1 | col2 | col3

100 | 0 | 100

100 | 0 | 100

0 | 150 | 150

0 | 200 | 200

200 | 0 | 200



i希望根据(上一行的col3) AND (当前行的col1和col2)更改col3 使用以下公式:



col3(当前行)= col3(上一行) - (col1(当前行)-col2(当前行))



此计算结果数据表之后必须如下:



col1 | col2 | col3

100 | 0 | 100

100 | 0 | 200

0 | 150 | 50

0 | 200 | -150

200 | 0 | 50



非常感谢

解决方案

 DataTable dtRawTable =  new  DataTable(); 
DataTable dtFinalised = new DataTable();
dtFinalised.Columns.Add( Col1 typeof int ));
dtFinalised.Columns.Add( Col2 typeof int ));
dtFinalised.Columns.Add( Col3 typeof int ));

for int index = 0 ; index < = dtRawTable.Rows.Count; index ++)
{
DataRow dr = dtFinalised.NewRow() ;
if (index%2 == 0
{
// col3(当前行)= col3(上一行) - (col1(当前行)-col2(当前行) )
// 请根据您的概念更改格式
dr [ Col1] =(Convert.ToInt32(dtRawTable.Rows [index-1] [ Col1]。ToString()) - (Convert.ToInt32(dtRawTable.Rows [index] [ Col1]。ToString()) - Convert.ToInt32(dtRawTable.Rows [index-1] [ Col2]。ToString())));
dr [ Col2] =(Convert.ToInt32(dtRawTable.Rows [index - 1 ] [ Col2]。 ToString()) - (Convert.ToInt32(dtRawTable.Rows [index] [ Col1]。 ToString()) - Convert.ToInt32(dtRawTable.Rows [index - 1 ] [ col2\" 的]的ToString())))。
dr [ Col3] =(Convert.ToInt32(dtRawTable.Rows [index - 1 ] [ Col3]。 ToString()) - (Convert.ToInt32(dtRawTable.Rows [index] [ Col1]。 ToString()) - Convert.ToInt32(dtRawTable.Rows [index - 1 ] [ col2\" 的]的ToString())))。
}
其他
{
dr [ Col1] = Convert.ToInt32(dtRawTable.Rows [index] [ Col1中]的ToString());
dr [ Col1] = Convert.ToInt32(dtRawTable.Rows [index] [ Col1]。ToString());
dr [ Col1] = Convert.ToInt32(dtRawTable.Rows [index] [ Col1]。ToString());
}
dtFinalised.Rows.Add(dr);
dtFinalised.AcceptChanges();


}







如果对您有用

,请将其标记为答案

i have a datatable with columns : col1, col2, col3
suppose dataTable have below content

col1 | col2 | col3
100 | 0 | 100
100 | 0 | 100
0 | 150 | 150
0 | 200 | 200
200 | 0 | 200

i want change col3 based on (col3 from previous row) AND (col1 and col2 from current row) with below formula :

col3(current row)=col3(previous row)-(col1(current row)-col2(current row))

after this calculation result datatable must like below :

col1 | col2 | col3
100 | 0 | 100
100 | 0 | 200
0 | 150 | 50
0 | 200 | -150
200 | 0 | 50

thanks a lot

解决方案

 DataTable dtRawTable = new DataTable();
                DataTable dtFinalised=new DataTable();
                dtFinalised.Columns.Add("Col1",typeof(int));
                dtFinalised.Columns.Add("Col2",typeof(int));
                dtFinalised.Columns.Add("Col3",typeof(int));

for (int index = 0; index <= dtRawTable.Rows.Count; index++)
  {
     DataRow dr = dtFinalised.NewRow();
     if(index%2==0)
     {
      //  col3(current row)=col3(previous row)-(col1(current row)-col2(current row))
     // Please change the formaula acc to you concept
 dr["Col1"] = (Convert.ToInt32(dtRawTable.Rows[index-1]["Col1"].ToString())-(Convert.ToInt32(dtRawTable.Rows[index]["Col1"].ToString())-Convert.ToInt32(dtRawTable.Rows[index-1]["Col2"].ToString())));
dr["Col2"] = (Convert.ToInt32(dtRawTable.Rows[index - 1]["Col2"].ToString()) - (Convert.ToInt32(dtRawTable.Rows[index]["Col1"].ToString()) - Convert.ToInt32(dtRawTable.Rows[index - 1]["Col2"].ToString())));
  dr["Col3"] = (Convert.ToInt32(dtRawTable.Rows[index - 1]["Col3"].ToString()) - (Convert.ToInt32(dtRawTable.Rows[index]["Col1"].ToString()) - Convert.ToInt32(dtRawTable.Rows[index - 1]["Col2"].ToString())));
       }
 else
   {
      dr["Col1"] =Convert.ToInt32(dtRawTable.Rows[index]["Col1"].ToString());
     dr["Col1"] = Convert.ToInt32(dtRawTable.Rows[index]["Col1"].ToString());
     dr["Col1"] = Convert.ToInt32(dtRawTable.Rows[index]["Col1"].ToString());
   }
   dtFinalised.Rows.Add(dr);
   dtFinalised.AcceptChanges();


 }




Please mark it a s Answer if it is useful to u


这篇关于根据c#中数据表中的前一行计算行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆