[Last Call] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2009-05-02
Medium Priority
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-
  • 2
LVL 11

Accepted Solution

srikanthreddyn143 earned 150 total points
ID: 24288088
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

ID: 24288444
Sorry It is in VB.Net . Thats an Idea.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month17 days, 22 hours left to enroll

830 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