---
title: "How to find max value of a column in a datatable?"  
description: "How to find max value of a column in a datatable?"  
author: "Mark Devid"  
published: 2014-01-27  
updated: 2014-01-27  
canonical: https://www.mindstick.com/forum/1881/how-to-find-max-value-of-a-column-in-a-datatable  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# How to find max value of a column in a datatable?

I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to find the [max value](https://www.mindstick.com/forum/159367/sql-to-calculate-number-of-values-for-each-day-based-on-a-max-value-from-previous-date) of a [column](https://www.mindstick.com/forum/33860/how-to-calculate-column-summary-in-sql-server) in a datatable.

Below is my code:-

var maxVal = dsloadReferralCodes.Tables["dtReferralCodesTable"].AsEnumerable().Max(r => Convert.ToBoolean(int.TryParse(r.Field<[string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp)>("ROWNUM"), out intROWNUM)) ? (int?)intROWNUM : null);

and below id the [error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing) i [am getting](https://www.mindstick.com/forum/34179/i-am-getting-error-while-assigning-the-data-to-the-array-list) while trying to get the max value and assigning it to intROWNUM

[Unable to cast object](https://answers.mindstick.com/qa/100480/unable-to-cast-object-of-type-dbcontext-models-users-to-type-iobjectcontextadapter) of type 'System.Decimal' to type 'System.String'

Can [someone help me](https://answers.mindstick.com/qa/42311/can-someone-help-me-with-this-question-about-political-parties) in resolving the issue. This has been troubling me since long.

Thanks in advance...

## Replies

### Reply by Pravesh Singh

Hi Mark,

You can get it in simple manner with DataTable.Select() :

```
DataRow [] dr =dsloadReferralCodes.Tables["dtReferralCodesTable"].Select("ROWNUM=
MAX(ROWNUM)");   if(dr !=null)    {     //
Console.WriteLine(dr[0]["RowNum"]);        int
maxVal=Convert.ToInt32(dr[0]["RowNum"]);    }
```


---

Original Source: https://www.mindstick.com/forum/1881/how-to-find-max-value-of-a-column-in-a-datatable

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
