Totaling and averaging column in datagridview [modified]
-
Hi guys, I've got a windows form with a datagridview linked to an access database. The third column in the datagridview is a list of numeric values. On load up I would like a textbox to display the average of all the values in this column. Can't quite figure it out. Here's the code I have written:
int Counter = 0; double AverageCalc = 0; while (Counter < datagridview.Rows.Count) { string Average = null; Average = datagridview.SelectedRows[Counter].Cells[2].Value.ToString(); AverageCalc = AverageCalc + Convert.ToDouble(Average); Counter++; } AverageCalc = AverageCalc / datagridview.Rows.Count; txtAverage.Text = AverageCalc.ToString();
It loops through once, and then fails at:Average = datagridview.SelectedRows[Counter].Cells[2].Value.ToString();
When Counter = 1 I'm not really sure what else to try, any advise would be greatly appreciated. Thanks Martinmodified on Wednesday, April 8, 2009 9:24 AM
-
Hi guys, I've got a windows form with a datagridview linked to an access database. The third column in the datagridview is a list of numeric values. On load up I would like a textbox to display the average of all the values in this column. Can't quite figure it out. Here's the code I have written:
int Counter = 0; double AverageCalc = 0; while (Counter < datagridview.Rows.Count) { string Average = null; Average = datagridview.SelectedRows[Counter].Cells[2].Value.ToString(); AverageCalc = AverageCalc + Convert.ToDouble(Average); Counter++; } AverageCalc = AverageCalc / datagridview.Rows.Count; txtAverage.Text = AverageCalc.ToString();
It loops through once, and then fails at:Average = datagridview.SelectedRows[Counter].Cells[2].Value.ToString();
When Counter = 1 I'm not really sure what else to try, any advise would be greatly appreciated. Thanks Martinmodified on Wednesday, April 8, 2009 9:24 AM
its because you have only one row SELECTED and you are looping through all rows in datagridview try
Average = datagridview.Rows[Counter].Cells[2].Value.ToString();
to loop through all rows or
while (Counter < datagridview.SelectedRows.Count)
to loop only through selected rows
-
Hi guys, I've got a windows form with a datagridview linked to an access database. The third column in the datagridview is a list of numeric values. On load up I would like a textbox to display the average of all the values in this column. Can't quite figure it out. Here's the code I have written:
int Counter = 0; double AverageCalc = 0; while (Counter < datagridview.Rows.Count) { string Average = null; Average = datagridview.SelectedRows[Counter].Cells[2].Value.ToString(); AverageCalc = AverageCalc + Convert.ToDouble(Average); Counter++; } AverageCalc = AverageCalc / datagridview.Rows.Count; txtAverage.Text = AverageCalc.ToString();
It loops through once, and then fails at:Average = datagridview.SelectedRows[Counter].Cells[2].Value.ToString();
When Counter = 1 I'm not really sure what else to try, any advise would be greatly appreciated. Thanks Martinmodified on Wednesday, April 8, 2009 9:24 AM
-
its because you have only one row SELECTED and you are looping through all rows in datagridview try
Average = datagridview.Rows[Counter].Cells[2].Value.ToString();
to loop through all rows or
while (Counter < datagridview.SelectedRows.Count)
to loop only through selected rows
-
its because you have only one row SELECTED and you are looping through all rows in datagridview try
Average = datagridview.Rows[Counter].Cells[2].Value.ToString();
to loop through all rows or
while (Counter < datagridview.SelectedRows.Count)
to loop only through selected rows
Thanks alot, I knew it would be something simple like that.