简体   繁体   English

如何使用For Loop两次查找数据表列和行?

[英]How do I use For Loop twice to find datatable columns and rows?

I want to use for loop to get average of datatable columns and rows. 我想使用循环来获取数据表列和行的平均值。 What I want to do is that what if there are 100 ~ 1000 columns and rows, I can't keep on adding them in the code. 我想做的是,如果有100〜1000列和行,我将无法继续在代码中添加它们。 is there one simple code that can get average of automatically as I add columns and rows? 是否有一个简单的代码可以在我添加列和行时自动获得平均值?

here is my code, I am stuck I don't know what to write in ?? 这是我的代码,我被卡住了,我不知道该写些什么? area below and this code gets me error please help... 下面的区域,这个代码让我错误请帮助...

private void button1_Click(object sender, EventArgs e)
    {
        DataTable dtGrid = gridData.DataSource as DataTable;
        DataTable dtResult = new DataTable();
        Math columnIndex = new Math();

        List<double> avgList = new List<double>();

        for (int i = 0; i < dtGrid.Columns.Count; i++)
        {

            for (int k = 1; k < dtGrid.Rows.Count; k++)
            {
                // ??
                avgList.Add(Convert.ToDouble(dtGrid.Rows[i].ToString()));
            }

        }
        //this is from other class name Math
        /* public double getAverageValue(List<double> avgList)
        {
            double averageList = 0;

            averageList = MathNet.Numerics.Statistics.Statistics.Mean(avgList.ToList());

            return averageList;
        }*/ 
        double averageX1 = columnIndex.getAverageValue(avgList);

        List<Math> list = new List<Math>();

        //using get; set from other class 
        list.Add(new Math { Result = "Average", X1 = averageX1.ToString() });

        gridData2.DataSource = list;
    }
}

} }

It looks like your loop is inside out. 看起来你的循环里面外面。 Try this: 尝试这个:

DataTable dtGrid = gridData.DataSource as DataTable;
DataTable dtResult = new DataTable();
Math columnIndex = new Math();

List<double> avgList = new List<double>();
for (int k = 1; k < dtGrid.Rows.Count; k++)
{
    for (int i = 0; i < dtGrid.Columns.Count; i++)
    {
        // ??
        avgList.Add(Convert.ToDouble(dtGrid.Rows[k].Columns[i].ToString()));
    }
}

This logic averages all columns in a row together. 此逻辑将一行中的所有列平均在一起。 If you need,, you can create a Dictionary and average each column separately. 如果需要,可以创建一个Dictionary并分别对每列进行平均。 Something like thisL 像这样的东西

Dictionary<int, List<double>> AvgColumnList = new Dictionary<int, System.Collections.Generic.List<double>>();

This uses a dictionary that contains a list for each column in the row. 这使用包含行中每列的列表的字典。 If there are 100 columns, then there will be 100 entries in the dictionary with index 0 - 99. Each dictionary item will contain a list of doubles. 如果有100列,则字典中将有100个条目,索引为0 - 99.每个字典项将包含双精度列表。

for (int k = 1; k < dtGrid.Rows.Count; k++)
{
    for (int i = 0; i < dtGrid.Columns.Count; i++)
    {
        if (!AvgColumnList.Keys.Contains(i))
            AvgColumnList.Add(i, new List<double>());

        AvgColumnList[i].Add(Convert.ToDouble(dtGrid.Rows[k].Columns[i].ToString()));
    }
}

DataTable is zero index based, in your code row count started from 1 it should be 0 , also dtGrid.Rows[i] is a row not the cell value. DataTable是基于零索引的,在你的代码行计数从1开始它应该是0 ,同样dtGrid.Rows[i]是一行而不是单元格值。 Use below code to loop through each cell of a DataTable 通过每个小区使用下面的代码回路DataTable

Update : Code updated as OP want to save each column data separately and irrespective of column numbers. 更新:更新为OP的代码希望分别保存每个列数据,而与列号无关。

 List<List<double>> perColumnAvg = new List<List<double>>();

 for (int i = 0; i < dtGrid.Columns.Count; i++)
 {
     avgList = new List<double>();
     for (int k = 0; k < dtGrid.Rows.Count; k++)
     {
         // ??
         avgList.Add(Convert.ToDouble(dtGrid.Rows[k][i].ToString()));
     }
     perColumnAvg.Add(avgList);
 }

Now you can compute individual column average as 现在,您可以将单个列平均值计算为

foreach (var columnList in perColumnAvg)
{
    // place your logic here.
    columnIndex.getAverageValue(columnList);
}

And can compute avg across table using. 并且可以使用跨表计算平均值。

double tableAvg = columnIndex.getAverageValue(perColumnAvg.SelectMany(s=>s));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM