• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

Getting a value in DataGrid within loop

I know this is something pretty simple but I can see what's wrong.  I need the eagle eye person to spot this out for me.  I have a shopping cart and I need to get the total quantities for all items in the cart.  Obviously, I'll need a loop and so here it is:

 If Page.IsPostBack = False Then

' Populate datagrid with shopping cart data
      Me.DataGrid1.DataSource = cart.GetItems(cartId)
      Me.DataGrid1.DataBind()

 ' Iterate through all rows within shopping cart list to find the total quantity
      Dim quantity As Integer
      Dim i As Integer
      For i = 0 To Me.DataGrid1.Items.Count - 1 Step i + 1
           Dim quantityTxt As TextBox = CType(Me.DataGrid1.Items(i).FindControl("Quantity"), TextBox)
           quantity = Int32.Parse(quantityTxt.Text)
      Next

End If

Whenever I go through the debugger, the variable 'quantityTxt' always equals nothing?  I'm not sure why but this throws an error on the next line.  

Any ideas?

Chad M Lawson
0
cmlawson
Asked:
cmlawson
  • 4
  • 4
1 Solution
 
laotzi2000Commented:
First why your step is i+1 instead of 1?
Second, try move the iteration code to Page_Prerender event handler
0
 
cmlawsonAuthor Commented:
I'm not sure why my step is i + 1?  I didn't really look at it that closely because I was dealing with the other section of code.  What do you recommend it be?

If I move the code to the Page_Prerender event, will I be able to acess the necessary values?  I mean, doesn't it have to bind in order for those values to be put in there?

thanks,

CML
0
 
laotzi2000Commented:
you can bind in page_load
and get the total in page_prerender
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
cmlawsonAuthor Commented:
Here's what I now have:

Public quantity As Integer

Private Sub Pre_Render(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.PreRender

      ' Iterate through all rows within shopping cart list to find the total quantity
       Dim i As Integer
       For i = 0 To Me.DataGrid1.Items.Count - 1 Step 1
            Dim quantityTxt As TextBox = CType(Me.DataGrid1.Items(i).FindControl("Quantity"), TextBox)
            quantity = Int32.Parse(quantityTxt.Text)
       Next

End Sub

I'm still getting 'nothing' for quantityTxt?

0
 
laotzi2000Commented:
how about just use
quantity = me.datagrid1.items(i).cells(2).text

note: 2 should be actual your quantity column index
0
 
cmlawsonAuthor Commented:
Well...will that work if there is more than one item in the cart?  I'm open to whatever will work and is easiest.  If I don't have the loop in there, how will I know the total of all the quantities?  Can you show me what you mean in code or psuedo code?

By the way, my dataGrid has columns ProductName, Price, Quantity, & Total so your guess as 2 for the column index is right.

thanks,

CML
0
 
laotzi2000Commented:
i think so.
try this one:

       Dim i As Integer
       dim quantity = 0
       For i = 0 To Me.DataGrid1.Items.Count - 1
             quantity += Int32.Parse( me.datagrid1.items(i).cells(2).text)
       Next
0
 
cmlawsonAuthor Commented:
Worked like a charm!  

Thanks so much.  I learn a little bit every time I post here.

great work.
0

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.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now