遍历DataTable中每行的每个值并比较该值

时间:2018-08-22 16:01:55

标签: c#

我正在尝试使用c#和exceldatareader读取然后导出一个复杂的excel表。

基本上,我想获取特定的行,例如第10到50行,然后遍历每行和该行中的值,以便可以逐个单元比较值。 例如,我抓取第10行,它具有固定的列数,因此我需要遍历该行中的每个单元格,抓取前4个,然后在此之后,如果我发现非0或null的值,则抓取该值,然后在特定单元格上方找到固定数量的行(找到了非null或0的值)并获取另一个值。

任何帮助将不胜感激。

这是我到目前为止所拥有的。我可以遍历所需的行,然后阅读前4列,然后其余的我只需要找到特定的单元格,然后在该特定单元格上方进行一些操作即可。解决这个问题的最有效方法是什么?

for (int i=15; i< 25; i++)
        {
            for( int j=1; j< 20; j++)
            {

                if(j < 4)
                {
                    Console.WriteLine(dt2.Rows[i][j]);

                }
                else
                {
                   //if you find any value other than 0 or null grab the position of this cell and then go to a specific cell in top most row.
                }
            }

        }

1 个答案:

答案 0 :(得分:0)

听起来您只需要像这样玩索引:

for (int i=15; i< 25; i++)
    {
        for( int j=1; j< 20; j++)
        {

            if(j < 4)
            {
                Console.WriteLine(dt2.Rows[i][j]);

            }
            else
            {
               if (dt2.Rows[i][j] does not equal 0 or null) 
               {
                   Console.WriteLine(dt2.Rows[i-some_fixed_#_of_rows][j]);
               }
               else
               {
                   ...
               }
            }
        }

    }

当然,您还必须检查i-some_fixed_#_of_rows是否不会超出您的范围。

相关问题