Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

i have the sample gridview data is as follows
which i am adding rows without using a database

id name sal

1 aaa 100

2 bbb 150

3 ccc 200

4 ddd 120

C#
if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int max_cols = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Columns"));

        }


need to write code for getting max value of the column

want to know the maximum value of the column ,i.e in above example the max value of sal 200

thanks inadvance
Posted
Updated 15-Nov-11 0:01am
v5

Sorting? Is the GridView bound? In that case you could copy and sort the datasource and get the highest value.
If you wish to get the highest value without sorting the actual grid and you cannot sort a datasource you could for each through the rows, get the value of sal, cast it to an integer and retrieve the highest number.
Using LINQ might also be an option here. That way you do not have to sort, but you can still get the Max value.
In VB (you didn't specify a language) something like:
VB
Dim highestValue = DataGridView1.Rows.OfType(Of DataGridViewRow).Max(Function(row) Convert.ToInt32(row.Cells("sal").Value))

In C# (from the top of my head):
C#
int highestValue = DataGridView1.Rows.OfType<DataGridViewRow>.Max(row => Convert.ToInt32(row.Cells("sal").Value))

This could also be applied directly to the datasource, if your grid is bound.
 
Share this answer
 
v3
Comments
Mani Zachariah 20-Nov-12 5:22am    
Hi,
Should we use any header file?I'm getting an error like this:
'System.Linq.ParallelEnumerable.OfType<TResult>(System.Linq.ParallelQuery)' is a 'method', which is not valid in the given context
Im new to C# please help?
Your grid is bound to a datasource so query it for a Max value, not the grid.
 
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