I've used this code to get the sum of the values of each column and display in footer. Here, the advantage is that I don't have to look for the column name, just get the count of total number of rows and columns and then iterate over each row and column.
Please suggest how I can make this code more efficient or if there is any way to make this code block more effective.
#region column sum and show in footer
//loop all the rows in the datatable
int[] RowTotals = new int[dt_.Columns.Count];
foreach (DataRow row in dt_.Rows)
{
if(dt_.Rows.IndexOf(row) !=0)
{
//loop all the columns in the row
for (int i = 1; i < dt_.Columns.Count; i++)
{
//add the values for each cell to the total
RowTotals[i] += Convert.ToInt32(row[dt_.Columns[i].ColumnName]);
}
}
}
//loop all the columns again to set the values in the footer
for (int i = 1; i < dt_.Columns.Count; i++)
{
GridView1.FooterRow.Cells[i].Text = string.Format("{0:N2}", RowTotals[i]);
}
///////
#endregion