0

I have loaded an excel file using OleDB connection. The code loads the excel file to a DataGridView. Now I want to use the values of this datatable, lets say sum, multiply all rows or columns. Over the net I have found only the static approach of doing this:

object 1 = DATATABLE.Compute("sum(NameOfTable)", "").ToString();

But since I load different excel files, I need a more genuine way. I have tried several different ways - add the values to a List or an Array but it does not seem to work. How can I achieve this?

keenthinker
  • 7,645
  • 2
  • 35
  • 45

1 Answers1

0

you just need to iterate over datacolumns

  List<string> l=new List<String>();
            foreach (DataColumn dc in table.Columns)
            {
              var Sum= table.AsEnumerable().Sum(x => x.Field<double>(dc));
              l.Add(Sum.ToString());
            }

you may need to check if datacolumn is null and contains numeric type or not otherwise casting will throw an exception. Here is a solution to check if datacolumn is numeric

Community
  • 1
  • 1
Kryptonian
  • 860
  • 3
  • 10
  • 26