Click here to Skip to main content
15,943,757 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I have a datatable which contains some rows and clumns.

Product           To (m)     
A                 12.6     
C                 30.1     
D                 40

Now I want to retrieve the maximum value Of To (m) column. To (m) has some NULL values also.
For example

Maximum To (m) =40

Can you please provide me a suitable solution for this?
Updated 4-Dec-13 4:46am

Try this, it will draw out those rows that hold the max value, and we just pick the first one and display it in a textbox. Null is not a concern here. I assume the column index of [To (m)] is 1.

DataRow[] dr = datatable.Select("[To (m)] = MAX([To (m)])");

textBox1.Text = dr[0][1].ToString();
Share this answer
Share this answer
Use [MSDN] DataTable.Select Method[^].
var maxRow = dt.Select("To = MAX(To)");

Here To is the DataTable Column Name. Replace your Column Name accordingly.
Share this answer
ErBhati 4-Dec-13 2:14am    
my column name is TO (M) . and it gives error
What is the error?
ErBhati 4-Dec-13 2:19am    
my column has NULL values also....
Can't you have a verification for nulls?
ErBhati 4-Dec-13 2:32am    
It gives me 12.6 according to my example not the max value of TO(M) column....

you can use Linq to achieve your requirement .

try this .

var query = dt.AsEnumerable().Max(x=>x[1]);

here i select the maximum value from second column .

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900