获取数据表c#中行的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1024211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:47:28  来源:igfitidea点击:

Get Value of Row in Datatable c#

c#datatablegradientdatarow

提问by subprime

i have a problem with my code.

我的代码有问题。

foreach (DataRow dr in dt_pattern.Rows)
    {
      part = dr["patternString"].ToString();
      if (part != vpart)
      {
        System.Console.WriteLine(part);
        System.Console.WriteLine("Geben Sie bitte für den Abschnitt die AT ein: ");
        temp = System.Console.ReadLine();
        AT = ToDouble(temp);
        dr["AT"] = AT;

        double xATmax = ToDouble(dr["Ampl"].ToString());
        double x = ToDouble(dr["Time"].ToString());

        double yATmax = ToDouble(dr["Ampl"]+1.ToString()) + AT;
        double y = ToDouble(dr["Ampl"].ToString());

        dr["alphaATmin"] = Gradient(x,xATmax,y,yATmax);
        System.Console.WriteLine(dr["alphaATmin"]);
      }
      vpart = part;          
    }

but i need at xATmax and yATmax the Value of the next Row... Someone can help me ?

但我需要 xATmax 和 yATmax 下一行的值......有人可以帮助我吗?

采纳答案by subprime

Dont use a foreach then. Use a 'for loop'. Your code is a bit messed up but you could do something like...

那就不要使用 foreach 了。使用“for 循环”。你的代码有点乱,但你可以做一些像......

for (Int32 i = 0; i < dt_pattern.Rows.Count; i++)
{
    double yATmax = ToDouble(dt_pattern.Rows[i+1]["Ampl"].ToString()) + AT;
}

Note you would have to take into account during the last row there will be no 'i+1' so you will have to use an if statement to catch that.

请注意,您必须考虑到在最后一行中将没有 'i+1',因此您必须使用 if 语句来捕获它。

回答by John Saunders

for (int i=0; i<dt_pattern.Rows.Count; i++)
{
    DataRow dr = dt_pattern.Rows[i];
}

In the loop, you can now reference row i+1 (assuming there is an i+1)

在循环中,您现在可以引用第 i+1 行(假设有一个 i+1)

回答by Justin

for (Int32 i = 1; i < dt_pattern.Rows.Count - 1; i++){ double yATmax = ToDouble(dt_pattern.Rows[i]["Ampl"].ToString()) + AT; }

for (Int32 i = 1; i < dt_pattern.Rows.Count - 1; i++){ double yATmax = ToDouble(dt_pattern.Rows[i]["Ampl"].ToString()) + AT; }

if you want to get around the + 1 issue

如果你想解决 +1 问题