什么样的数据类型是count
start
和end
声明为?这不会显示在您的代码中。
此外,您提供的代码(如果有效)只会为您提供第一个和最后一个元素,而不是集合中的最小值和集合中的最大值。如果您正在寻找最小/最大,你需要写一个循环,像这样的:
// minimum
int min = dt.Rows[0][2]; // assuming you want the third column (index 2)
for(int i = 1; i < dt.Rows.Count; i++)
{
if(min > (int)dt.Rows[i][2]) min = (int)dt.Rows[i][2];
}
// maximum
int max = dt.Rows[0][2]; // assuming you want the third column (index 2)
for(int i = 1; i < dt.Rows.Count; i++)
{
if(max < (int)dt.Rows[i][2]) max = (int)dt.Rows[i][2];
}
显然,这些也可以组合成一个循环:
// minimum and maximum
int max = dt.Rows[0][2]; // assuming you want the third column (index 2)
int min = dt.Rows[0][2]; // assuming you want the third column (index 2)
for(int i = 1; i < dt.Rows.Count; i++)
{
if(max < (int)dt.Rows[i][2]) max = (int)dt.Rows[i][2];
if(min > (int)dt.Rows[i][2]) min = (int)dt.Rows[i][2];
}
@marc_s'答案对于您的特定用例(数据表)更为优雅,但是我的工作将适用于索引集合中的任何数据集。
你看到什么异常? –
@David“位置0没有行”这是我得到的第二行的异常......并且数据表已满..它不是空的.. – vince