I want to get the sum and also the average of GridView column .If there is any "null" entries in the column then it is not give me the sum.
int i = 0;
int j = 0;
int sum = 0;
try
{
for (i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (j = 0; j < dataGridView1.Columns.Count; j++)
{
sum = sum + (Convert.ToInt32(dataGridView1.Rows[i].Cells[j].Value));
}
}
MessageBox.Show("sum"+sum);
}
int j = 0;
int sum = 0;
try
{
for (i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (j = 0; j < dataGridView1.Columns.Count; j++)
{
sum = sum + (Convert.ToInt32(dataGridView1.Rows[i].Cells[j].Value));
}
}
MessageBox.Show("sum"+sum);
}
I want to get average of the column and then place it in the null value cell.
any body help me
Raghav JhavarPosted Dec 17, 2015, 4:18 AM
Suppose your column number you want to get sum of and average is '1':
You can do:
int sum = 0;
foreach(DataGridViewRow DR in dataGridView1.Rows)
Console.WriteLine(sum);
Ranjit PowarPosted Dec 16, 2015, 9:48 PM
{
for (i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (j = 0; j < dataGridView1.Columns.Count; j++)
{
if(dataGridView1.Rows[i].Cells[j].Value!=null)
{
sum = sum + (Convert.ToInt32(dataGridView1.Rows[i].Cells[j].Value));
}
}
}
MessageBox.Show("sum"+sum);
}