Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何计算行中的每个单元格_C# - Fatal编程技术网

C# 如何计算行中的每个单元格

C# 如何计算行中的每个单元格,c#,C#,如何计算行中的每个单元格并将计算的数据添加到名为“%bin”的新列中 原始数据表: In system | Bin1 | Bin2 | Bin3 | ... | Bin100 | ----------|------|------|------|-----|--------| 3598 | 3590 | 0 | 5 | ... | 3 | 1500 | 1495 | 3 | 2 | ... | 0 | 我想做以下几点: In s

如何计算行中的每个单元格并将计算的数据添加到名为“%bin”的新列中

原始数据表:

In system | Bin1 | Bin2 | Bin3 | ... | Bin100 |
----------|------|------|------|-----|--------|
3598      | 3590 |  0   |  5   | ... |   3    | 
1500      | 1495 |  3   |  2   | ... |   0    | 
我想做以下几点:

In system | Bin1 | Bin2 | Bin3 | ... | Bin100 | %Bin1 | %Bin2 | ...
----------|------|------|------|-----|--------|-------|-------|------
3598      | 3590 |  0   |  5   | ... |   3    | 0.99  |   0   | ...
1500      | 1495 |  3   |  2   | ... |   0    | 0.99  | 0.002 | ...
霉菌代码:

int i = 0;
int[] sum = new int[101];  
for (i = 1; i < 101; i++)
{
  sum[i] = Convert.ToInt32(dt.Compute("Sum(BIN" + i.ToString() + ")",string.Empty));
  if(sum[i] == 0 )
  {
     dt.Columns.Remove("BIN"+i.ToString()+"");
  }
  else
  {
     dt.Columns.Add("Total"+i.ToString()+"", typeof(decimal));
     foreach (DataRow row in dt.Rows)
     {
       decimal rowSum = 0;
       string stringsystem = row["In System"].ToString();
       int system = Convert.ToInt32(stringsystem);
       string stringBinnum = row["BIN" + i.ToString() + ""].ToString();
       int Binnum = Convert.ToInt32(stringBinnum);
       decimal d;
       d = (Binnum / system) * 100;
       rowSum = d;

       row.SetField("%Bin"+i.ToString()+"", rowSum);
     }
  }
}
inti=0;
int[]和=新的int[101];
对于(i=1;i<101;i++)
{
sum[i]=Convert.ToInt32(dt.Compute(“sum(BIN“+i.ToString()+”),string.Empty));
如果(和[i]==0)
{
dt.Columns.Remove(“BIN”+i.ToString()+”);
}
其他的
{
dt.Columns.Add(“Total”+i.ToString()+”,typeof(decimal));
foreach(数据行中的数据行)
{
十进制行和=0;
字符串stringsystem=行[“在系统中]。ToString();
int system=Convert.ToInt32(stringsystem);
字符串stringBinnum=行[“BIN”+i.ToString()+“”].ToString();
int Binnum=Convert.ToInt32(stringBinnum);
十进制d;
d=(宾数/系统)*100;
行和=d;
row.SetField(“%Bin”+i.ToString()+”,rowSum);
}
}
}

您在使用此代码时遇到了什么问题?如何计算
%Bin1
%Bin2
等的值?结果不正确,只计算第一列@chetanranpariya您可以使用表达式在datatable中创建计算列。看这里