Dim MaxVal As Integer

For a = 0 To DataGridView1.RowCount - 1
If MaxVal < DataGridView1.Item(DataGridView1.ColumnCount - 1, a).Value Then
MaxVal = CInt(DataGridView1.Item(DataGridView1.ColumnCount - 1, a).Value)
End If
Next
MsgBox("The Optimum Value is " & MaxVal, 1, "Optimum Choice")

Recommended Answers

All 14 Replies

What is your question about the code you posted?

Just displaying code is not enough for us to understand ur problem....do explain ur problem in detail...

opssss. sorry! actually the code will dispaly the maximum number on datagridview. now, i want to display which row it is at message box. House 1 & 2 refer to the row name.

" The optimum value is 13 at House 1 and House 2"

" The optimum value is 13 at House 1 and House 2"

In this is 13 ur row number??

As I understood u want to display the row number of the item which will be clicked

Dim MaxVal As Integer
For a = 0 To DataGridView1.RowCount - 1
    MaxVal = DataGridView1.CurrentCell.RowIndex + 1
Next
MsgBox("The Optimum Value is " & MaxVal, 1, "Optimum Choice")

Please correct me if u need something else....

nope. actually i have 10 rows, label as House 1 until House 10. the comand do to check the highest number (MaxVal).

now, i want msgbox prompt the output as " The optimum number is 13 at House 1, House 2"

optimum number refers to the highest number. u can refer to the attachment. tq =D

What does ur MaxVal stores??? no attachment displayed

as i understand is that you have 10 rows and you have a column name house and a optimum value
now you just want to get the largest optimum value of any house ? if i m right then you can do like this.

here is code if there is single column of optimum value in your grid.

    dim maxVal as integer
     maxVal = 0 ;
     for i = 0 to datagridview1.rows.count -1 
        if maxVal < datagridview1.item(2,i).value.tostring then
        maxVal = datagridview1.item(2,i).value.tostring
        end if
     next
    msgbox ("your string here" & maxVal & " your string here.")

here is code if there is more then one column in the grid of optimum value ,

  dim maxVal as integer
     maxVal = 0 ;
     for i = 0 to datagridview1.rows.count -1 
         for a = 0 to datagridview1.column.count -1 
           if maxVal < datagridview1.item(a,i).value.tostring then
              maxVal = datagridview1.item(a,i).value.tostring
           end if    
         next
    next
    msgbox ("your string here" & maxVal & " your string here.")

as i typed all this code here so may be there is some mistake in it , so please if you find any do tell me here so that i can solve it , one more thing , please always post question with full description, so that you can get good an fast help and also post attachment if possible.

Best Regards

Muhammad Waqas Aslam

you're right Waqas. other than show the maxValue, i want to show the Row name that refer to the maxValue.

but the code given have one error which is

Error 1 'column' is not a member of 'System.Windows.Forms.DataGridView'.

here are the attachement. u can see the maxvalue in last column.

Create a second var to hold the house number. Then when you set the maxVal you'll set the house as well.

yup. but since i dont know how to do the Rows code, i'll need sumone to help. =D

Dim house string 
For a = 0 To DataGridView1.RowCount - 1
If MaxVal < DataGridView1.Item(DataGridView1.ColumnCount - 1, a).Value Then
MaxVal = CInt(DataGridView1.Item(DataGridView1.ColumnCount - 1, a).Value)
house = DataGridView1.Item(1,a) 'Change 1 to reflect the correct column 
elseif MaxVal = DataGridView1.Item(DataGridView1.ColumnCount - 1, a).Value Then 
house &= ", " & DataGridView1.Item(1,a) 'Change 1 to reflect the correct column 
End If
Next
MsgBox("The Optimum Value is " & MaxVal & " in houses: " & house , 1, "Optimum Choice")

please change this line like this

for a = 0 to datagridview1.columns.count -1

hope this will solve your prob :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.