How do I find that max/min value of a cell in a DataTable (C#)

Posted on 2009-05-02
Last Modified: 2013-12-17
I'm looking for a way to find the min/max value of a group of cells in a DataTable, regardless of the datatype of the specific columns.  I could be given a DataTable populated with cells of various datatypes (int32, decimal, double, ...).

For example, I could be given the following:

| 10 | 83 | 21 |
| 22 | 17 | 92 |
| 42 | 33 | 89 |

Min: 10
Max: 92

Or for example:

| 3.2 | 9.1 | 6.8 |
| 1.2 | 7.5 | 8.3 |

Min: 1.2
Max: 9.1

Surely there is a way to do this in a fashion more automated than checking the 'type' of the column and using the specific System.Convert function?  Ideally the result should be returned in the datatype of the cells in the table.

I went down a path of creating a class for each datatype, and doing class specific interpretation -- but that doesn't feel right.

Question by:-DLinder-
    LVL 11

    Accepted Solution

    You can save all the cell data in a arraylist and sort it.

    Dim alItems As New Arraylist
    For each row in Tbl.Rows
    and so on
    'You can have a for loop inside to automatically add into arraylist. instead of adding individually
    MinValue = alItems.Item(0)
    MaxValue = alItems.Item(alItems.Size - 1)
    LVL 11

    Expert Comment

    Sorry It is in VB.Net . Thats an Idea.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    Refresh page ASP.NET C# 3 39
    Gridview Edit Row 10 34
    VB.NET Server side Javascript 8 43
    Text in tooltip bound field bridview 3 16
    Flash ( has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
    Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    6 Experts available now in Live!

    Get 1:1 Help Now