Click here to Skip to main content
15,895,557 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     
B                  
C                 30.1     
D                 40
E                 

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?
Posted
Updated 4-Dec-13 4:46am
v4

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.

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

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

Here To is the DataTable Column Name. Replace your Column Name accordingly.
 
Share this answer
 
Comments
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....
Hello,

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 .

thanks
 
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