Question DataTable.Compute?

rapmaster

New member
Joined
Aug 25, 2015
Messages
1
Programming Experience
Beginner
Hello guys,
I hope that I have chosen the correct category for my question, but if not, please excuse me I am new. So to the point... I have loaded a excel file using OleDB connection. The code loads the excel file to a dataGridView. Now I want to use the values of the datatable, lets sum, multiply, compare and so on. Over the net I have found only the static way of doing this
object 1 = DATATABLE.Compute("sum(NameOfTable)", "").ToString();

But since i load different excel files, i need a more genuine way.
Thanks in advance
 
Hello guys,
I hope that I have chosen the correct category for my question, but if not, please excuse me I am new.

The DataTable.Compute method has nothing to do with OLE DB specifically so questions about it belong in an ADO.NET forum. I'm not sure where this question will lead us just yet though, so I will leave it here for now.
 
So to the point... I have loaded a excel file using OleDB connection. The code loads the excel file to a dataGridView. Now I want to use the values of the datatable, lets sum, multiply, compare and so on. Over the net I have found only the static way of doing this
object 1 = DATATABLE.Compute("sum(NameOfTable)", "").ToString();

But since i load different excel files, i need a more genuine way.
Thanks in advance

I'm not really sure what you mean by "genuine". The point of the Compute method is to compute the result of an aggregate function executed against the contents of a DataTable, much as you would compute an aggregate in SQL code. It's a bit more limited than a true SQL implementation though, and the documentation can tell you what it can and can't do.

Your code doesn't really make sense because you're calling Compute on a table and you need to specify the column you want to sum, not the table. If you want to sum the products of two columns then you specify the product of those two columns as the argument to sum. Again, read the documentation first to see what the method can and can't do. If you find that it can't do what you need then post back and provide a FULL and CLEAR description of what you want because what you've said is a bit vague. It may be that you need to use a particular SQL query or you may have to loop through the Rows of the DataTable or you may need to use Office Automation in complex cases.
 

Latest posts

Back
Top Bottom