Click here to Skip to main content
15,886,592 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am developing window application . Im importing few text files to datagridview1.
Its each colum contain about 220 rows .Now i want to calculate average of all the column in next datagridview2 .

after that want to transpose the datagridview2 average values vertically in next datagridview3
then next want to calculate the total count of datagridview3 values that we have transposed in the next datagridview4.

next now calculate the standard deviation of those average values
I have written this code Im listing
but in this code not getting the correct answer. pls correct it

C#
public DataTable computedTable(DataTable dt)
{
    DataTable avgtable = new DataTable();
    List<double> values = new List<double>();
    double value = 0.00;
    double sumForMean = 0.00, bigSum = 0.00, mean = 0.00;
    double stdDev = 0.00;
    double minval = 0.00;
    double maxval = 0.00;
    bool b = true;
    var meanrow = avgtable.NewRow();
    var countrow = avgtable.NewRow();
    var stddevrow = avgtable.NewRow();
    var maxrow = avgtable.NewRow();
    var minrow = avgtable.NewRow();
    for (int col = 0; col < dt.Columns.Count; col++)
    {
         value = 0.00;
         sumForMean = 0.00; bigSum = 0.00; mean = 0.00;
         stdDev = 0.00;
        maxval=0.00;minval=0.00;
        avgtable.Columns.Add(dt.Columns[col].ColumnName);
        for (int row = 0; row < dt.Rows.Count; row++)
        {
            values.Add( double.Parse(dt.Rows[row][col].ToString()));
        }

        // Calculate the total for the mean
        for (int i = 0; i < values.Count; i++)
            sumForMean += values[i];
        // Calculate the mean
        mean = sumForMean / values.Count;

        // Calculate the total for the standard deviation
        for (int i = 0; i < values.Count; i++)
            bigSum += Math.Pow(values[i] - mean, 2);
        // Now we can calculate the standard deviation
        stdDev = Math.Sqrt(bigSum / (values.Count - 1));

        //Now Calculate Max Value of each column
        maxval = mean + this.Tvalue(double.Parse(values.Count.ToString())-1)*(stdDev/(Math.Sqrt(double.Parse(values.Count.ToString()))));
        minval = mean - this.Tvalue(double.Parse(values.Count.ToString())-1) * (stdDev / (Math.Sqrt(double.Parse(values.Count.ToString()))));
if (b == true)        
        if (b)
        {         
            avgtable.Rows.Add(meanrow);
            avgtable.Rows.Add(countrow);
            avgtable.Rows.Add(stddevrow);
            avgtable.Rows.Add(maxrow);
            avgtable.Rows.Add(minrow);
        }
        meanrow[col] = mean;
        countrow[col] = values.Count.ToString(); 
        stddevrow[col] =stdDev;
        maxrow[col] = maxval;
        minrow[col] = minval;
        b = false;
        values.Clear();
    }

    values.Add(value);
    
    return avgtable;
}
Posted
Updated 18-Sep-14 18:44pm
v4
Comments
OriginalGriff 18-Sep-14 8:34am    
And?
What have you tried?
Where are you stuck?
What help do you need?

Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.

1 solution

Hopefully you have used a DataTable as the data source for your DataGridView.
Then you can use this method to get the IQueryable interface from the data table.
C#
DataTable dt = datagridview1.DataSource;
var query = dt.AsEnumerable().AsQueryable();
double average = query.Average(row => (int)row["column_name"]); // If the column data type is an int


As for other statistical calculations you can use this CP article as a guide Using LINQ to Calculate Basic Statistics[^]
 
Share this answer
 
v2
Comments
meenakshi syal 19-Sep-14 0:13am    
I have this problem and this is my code :


about datagrid view in c# calculating statistical calculations
See more: C#3.5
I am developing window application . Im importing few text files to datagridview1.
Its each colum contain about 220 rows .Now i want to calculate average of all the column in next datagridview2 .

after that want to transpose the datagridview2 average values vertically in next datagridview3
then next want to calculate the total count of datagridview3 values that we have transposed in the next datagridview4.

next now calculate the standard deviation of those average values
I have written this code Im listing
but in this code not getting the correct answer. pls correct it


public DataTable computedTable(DataTable dt)
{
DataTable avgtable = new DataTable();
List values = new List();
double value = 0.00;
double sumForMean = 0.00, bigSum = 0.00, mean = 0.00;
double stdDev = 0.00;
double minval = 0.00;
double maxval = 0.00;
bool b = true;
var meanrow = avgtable.NewRow();
var countrow = avgtable.NewRow();
var stddevrow = avgtable.NewRow();
var maxrow = avgtable.NewRow();
var minrow = avgtable.NewRow();
for (int col = 0; col < dt.Columns.Count; col++)
{
value = 0.00;
sumForMean = 0.00; bigSum = 0.00; mean = 0.00;
stdDev = 0.00;
maxval=0.00;minval=0.00;
avgtable.Columns.Add(dt.Columns[col].ColumnName);
for (int row = 0; row < dt.Rows.Count; row++)
{
values.Add( double.Parse(dt.Rows[row][col].ToString()));
}

// Calculate the total for the mean
for (int i = 0; i < values.Count; i++)
sumForMean += values[i];
// Calculate the mean
mean = sumForMean / values.Count;

// Calculate the total for the standard deviation
for (int i = 0; i < values.Count; i++)
bigSum += Math.Pow(values[i] - mean, 2);
// Now we can calculate the standard deviation
stdDev = Math.Sqrt(bigSum / (values.Count - 1));

//Now Calculate Max Value of each column
maxval = mean + this.Tvalue(double.Parse(values.Count.ToString())-1)*(stdDev/(Math.Sqrt(double.Parse(values.Count.ToString()))));
minval = mean - this.Tvalue(double.Parse(values.Count.ToString())-1) * (stdDev / (Math.Sqrt(double.Parse(values.Count.ToString()))));
if (b == true)
{

avgtable.Rows.Add(meanrow);
avgtable.Rows.Add(countrow);
avgtable.Rows.Add(stddevrow);
avgtable.Rows.Add(maxrow);
avgtable.Rows.Add(minrow);
}
meanrow[col] = mean;
countrow[col] = values.Count.ToString();
stddevrow[col] =stdDev;
maxrow[col] = maxval;
minrow[col] = minval;
b = false;
values.Clear();
}

values.Add(value);


return avgtable;
}
George Jonsson 19-Sep-14 0:46am    
Have you even looked into my answer?
harshavardhan12345678 19-Sep-14 0:44am    
Have u got the Answer
meenakshi syal 22-Sep-14 6:53am    
no not yet

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900