?
Solved

Getting a value in DataGrid within loop

Posted on 2005-03-24
8
Medium Priority
?
200 Views
Last Modified: 2010-04-07
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
Comment
Question by:cmlawson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13623812
First why your step is i+1 instead of 1?
Second, try move the iteration code to Page_Prerender event handler
0
 

Author Comment

by:cmlawson
ID: 13623872
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
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13623897
you can bind in page_load
and get the total in page_prerender
0
Industry Leaders: 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!

 

Author Comment

by:cmlawson
ID: 13624210
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
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13624261
how about just use
quantity = me.datagrid1.items(i).cells(2).text

note: 2 should be actual your quantity column index
0
 

Author Comment

by:cmlawson
ID: 13624303
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
 
LVL 12

Accepted Solution

by:
laotzi2000 earned 2000 total points
ID: 13624318
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
 

Author Comment

by:cmlawson
ID: 13624446
Worked like a charm!  

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

great work.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses
Course of the Month13 days, 6 hours left to enroll

777 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