Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Loop Question

Loop Question

Scheduled Pinned Locked Moved Visual Basic
question
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Makniteasy
    wrote on last edited by
    #1

    i need to loop some rows in my datagrid to add them and show the total. i am using: Dim dr As DataRow Dim Amount As Double = 0 For Each dr As System.Data.DataRow _ In ds.Tables(0) dr = dr + ds.Tables(0).Rows.Item("Amount") Next Me.Text = ("My Balance $") & Amount.ToString everything is fine except the underlined part. it keeps saying that it is a member of system.data.datatable which is not a collection type. does anyone know what i am doing wrong? Makniteasy

    J 1 Reply Last reply
    0
    • M Makniteasy

      i need to loop some rows in my datagrid to add them and show the total. i am using: Dim dr As DataRow Dim Amount As Double = 0 For Each dr As System.Data.DataRow _ In ds.Tables(0) dr = dr + ds.Tables(0).Rows.Item("Amount") Next Me.Text = ("My Balance $") & Amount.ToString everything is fine except the underlined part. it keeps saying that it is a member of system.data.datatable which is not a collection type. does anyone know what i am doing wrong? Makniteasy

      J Offline
      J Offline
      Jim Matthews
      wrote on last edited by
      #2

      Tables(0) is not a collection, it's a specific table.

      ...
      For each r as Datarow in ds.Tables(0).Rows
      ...
      

      by the way your initial declaration of dr is reduntant since you are initializing it again within your for statement. just thought i'd mention it.


      -jim

      M 1 Reply Last reply
      0
      • J Jim Matthews

        Tables(0) is not a collection, it's a specific table.

        ...
        For each r as Datarow in ds.Tables(0).Rows
        ...
        

        by the way your initial declaration of dr is reduntant since you are initializing it again within your for statement. just thought i'd mention it.


        -jim

        M Offline
        M Offline
        Makniteasy
        wrote on last edited by
        #3

        thanks that fixed that problem but now i have another: Dim Amount As Double = 0 For Each dr As DataRow In ds.Tables(0).Rows dr = dr + ds.Tables(0).item("Amount") Next Me.Text = ("My Balance $") & ("Amount").ToString now this is underlined. i think im lost in the loop now.any more suggestions? Makniteasy

        J D 2 Replies Last reply
        0
        • M Makniteasy

          thanks that fixed that problem but now i have another: Dim Amount As Double = 0 For Each dr As DataRow In ds.Tables(0).Rows dr = dr + ds.Tables(0).item("Amount") Next Me.Text = ("My Balance $") & ("Amount").ToString now this is underlined. i think im lost in the loop now.any more suggestions? Makniteasy

          J Offline
          J Offline
          Jim Matthews
          wrote on last edited by
          #4

          can you give us an idea of exactly what it is you're trying to do? the code you've provided doesn't really make much sense. just a brief description should help.


          -jim

          1 Reply Last reply
          0
          • M Makniteasy

            thanks that fixed that problem but now i have another: Dim Amount As Double = 0 For Each dr As DataRow In ds.Tables(0).Rows dr = dr + ds.Tables(0).item("Amount") Next Me.Text = ("My Balance $") & ("Amount").ToString now this is underlined. i think im lost in the loop now.any more suggestions? Makniteasy

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            You're not adding the amounts. You're actually trying to add a Column to a DataRow. I think you're looking for:

            Dim Amount As Double = 0
            For Each dr As DataRow In ds.Tables(0).Rows
            Amount += dr.Item("Amount")
            Next
            Me.Text = "My Balance $" & Amount.ToString()

            RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            M 1 Reply Last reply
            0
            • D Dave Kreskowiak

              You're not adding the amounts. You're actually trying to add a Column to a DataRow. I think you're looking for:

              Dim Amount As Double = 0
              For Each dr As DataRow In ds.Tables(0).Rows
              Amount += dr.Item("Amount")
              Next
              Me.Text = "My Balance $" & Amount.ToString()

              RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              M Offline
              M Offline
              Makniteasy
              wrote on last edited by
              #6

              thanks for the help. The help you provided was exactly what i needed. i was trying to get the amount column of a datagrid, for which the user enters the amount they paid, to show in the datagrid caption text when entered. I am going to post another question so that you wont have to keep to returning this one. thanks Makniteasy

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups