Find value in grid

Arg81

Well-known member
Joined
Mar 11, 2005
Messages
949
Location
Midlands, UK
Programming Experience
1-3
hello peeps

I have a grid that lists a number of ingredients used in a recipe.

Now, a column in that grid is called ByProduct. An ingredient is either ByProductA or ByProductZ.

ByProductA overrides Z. I.E. There may be 20 ingredients, 19 are ByProductZ and only 1 is ByProductA.

Therefore the final ByProduct is A, not Z.

What I need to do is write some code that looks through each row of the grid, and if A exists in that column then a label on the form displays Value1. If it doesn't exist the label displays Value2.
 
I've just realised a mistake in the post about, Z should override A.

Here's a screenshot.

The number of rows vary depending on the recipe. However, all I need to do is "scan" the ByProduct column, and see whether ByProductZ exists in any of the rows.

Is there a simple way? Or would it be easier to search through the returned data in the dataTable?
 

Attachments

  • Image3.jpg
    Image3.jpg
    29 KB · Views: 34
Got it!!!

Probably not the nicest way to do it but it works. I used the same kind of code that I work out my column totals up with...

VB.NET:
Dim varByProd as Boolean
Dim rows As Integer = Me.grdRecipeIngredients.Splits(0).Rows.Count

varByProd = False

Dim i As Integer
For i = 0 To rows - 1
    Dim s As String = Me.grdRecipeIngredients(i, "IngredientByProdID")
            If s = "2" Then
                varByProd = True
            End If
Next i

If varByProd = True Then
         Me.lblByProduct.Text = "By-Product Z"
Else
         Me.lblByProduct.Text = "By-Product A"
End If

so the code sets varByProd to False immediately. It then loops through each row, and if any row has 2 in the ByProd column (2 = ByProd Z) then it makes varByProd True.
Once looped through, if varByProd = True then the label displays the correct value...


...finally sussed. It was seriously making me go crazy.

Now awaiting someone to post the most simplistic code to get the same job done :D (I prefer it the hard way ;) )
 
Hi Arg,

i've tried to find a simpler solution on the net but of everything i've seen yours is the most simple and least amount of code.

If you do see a simpler version please post back as I will no doubt need something like this in future,

Thanks

John
 
Or this:

VB.NET:
If underlyingTable.Select("[IngredientByProdID] = 2").Length = 0 Then
  lblByProduct.Text = "By-Product Z"
Else
  lblByProduct.Text = "By-Product A"
EndIf
 
Back
Top