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. C#
  4. iterate over datagrid

iterate over datagrid

Scheduled Pinned Locked Moved C#
htmlcssquestion
8 Posts 4 Posters 1 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.
  • K Offline
    K Offline
    kloepper
    wrote on last edited by
    #1

    I have a datagrid with this structure: Column1 Column2 Column3 Name URL FilePathtoSaveTo There are 1000 rows in the grid and I want to use the url (Column2) in an httpwebrequest and httpwebresponse to get web pages and then use the FilePathtoSaveTo (Column3) to write the web page to disk. Anyone know of a foreach loop (or some other way) that will allow me to do this (return the cell values from columns 2 and 3 in order to be used as variables for my web request and write?) Thanks, Paul (Also, the urls return text files that when saved with the .html extension, open as html files.)

    R D 2 Replies Last reply
    0
    • K kloepper

      I have a datagrid with this structure: Column1 Column2 Column3 Name URL FilePathtoSaveTo There are 1000 rows in the grid and I want to use the url (Column2) in an httpwebrequest and httpwebresponse to get web pages and then use the FilePathtoSaveTo (Column3) to write the web page to disk. Anyone know of a foreach loop (or some other way) that will allow me to do this (return the cell values from columns 2 and 3 in order to be used as variables for my web request and write?) Thanks, Paul (Also, the urls return text files that when saved with the .html extension, open as html files.)

      R Offline
      R Offline
      Roman Rodov
      wrote on last edited by
      #2

      Well WHAT is your data source for the Grid? You need to iterate over your data source, NOT the data grid

      K 1 Reply Last reply
      0
      • K kloepper

        I have a datagrid with this structure: Column1 Column2 Column3 Name URL FilePathtoSaveTo There are 1000 rows in the grid and I want to use the url (Column2) in an httpwebrequest and httpwebresponse to get web pages and then use the FilePathtoSaveTo (Column3) to write the web page to disk. Anyone know of a foreach loop (or some other way) that will allow me to do this (return the cell values from columns 2 and 3 in order to be used as variables for my web request and write?) Thanks, Paul (Also, the urls return text files that when saved with the .html extension, open as html files.)

        D Offline
        D Offline
        DougW48
        wrote on last edited by
        #3

        bool keepGoing = true; for (int rowNum = 0; keepGoing; rowNum++) { try { /* Do Stuff with dataGrid[rowNum][2] and dataGrid[rowNum][3] */ } catch { keepGoing=false; } } This will probably work, but I don't particularly like it...if you have a dataset with the information, you might want to iterate through that instead of the grid itself. - D

        K 1 Reply Last reply
        0
        • R Roman Rodov

          Well WHAT is your data source for the Grid? You need to iterate over your data source, NOT the data grid

          K Offline
          K Offline
          kloepper
          wrote on last edited by
          #4

          Sorry, it is a dataset. The main point is that I want to take a record then assign the value from that row, column2 to the variable that contains the url and then assign the value from that same row and column3 to the variable that will hold the pathname.filename to save as. I just haven't figured out exactly how to do it yet. (I've actually got to run now, but I'll be back later tonight) Thanks, Paul

          C 1 Reply Last reply
          0
          • D DougW48

            bool keepGoing = true; for (int rowNum = 0; keepGoing; rowNum++) { try { /* Do Stuff with dataGrid[rowNum][2] and dataGrid[rowNum][3] */ } catch { keepGoing=false; } } This will probably work, but I don't particularly like it...if you have a dataset with the information, you might want to iterate through that instead of the grid itself. - D

            K Offline
            K Offline
            kloepper
            wrote on last edited by
            #5

            So I simply designate the cell by iterating over the rows and then designating the column like this dataGrid[rowNum][2]? That simple? I'm using a dataset. Thanks, Paul (I've got to go now...be back later tonight.)

            D 1 Reply Last reply
            0
            • K kloepper

              So I simply designate the cell by iterating over the rows and then designating the column like this dataGrid[rowNum][2]? That simple? I'm using a dataset. Thanks, Paul (I've got to go now...be back later tonight.)

              D Offline
              D Offline
              DougW48
              wrote on last edited by
              #6

              Actually, I messed the syntax up. You can get the value of a cell with: dataGrid[rowNum, colNum] I agree with the other post about using the dataset instead of the grid...but if you have to, you can use the grid. Keep in mind that if you use the datagrid instead of the dataset, you will have to find a way of counting the rows. This is why I used the try/catch block of stopping the loop in my example. I'm sure there are better ways...I'm just too lazy tonight to look anything up. Also, if you use the datagrid, you may have to check the values you get from the cells to make sure they are usable. If the grid is editable, you'll (almost) always have that extra row at the bottom that will have nothing, but you'll still be able to return the value from those cells. Good luck! - D

              1 Reply Last reply
              0
              • K kloepper

                Sorry, it is a dataset. The main point is that I want to take a record then assign the value from that row, column2 to the variable that contains the url and then assign the value from that same row and column3 to the variable that will hold the pathname.filename to save as. I just haven't figured out exactly how to do it yet. (I've actually got to run now, but I'll be back later tonight) Thanks, Paul

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                Something like this:

                DataTable table = theDataSet.Tables[0];
                foreach(Row aRow in table.Rows)
                {
                string Url = aRow[1];
                string SavePath = aRow[2];
                }

                You can also use the name of the column instead of the ordinal position. Does this help?


                "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

                K 1 Reply Last reply
                0
                • C Colin Angus Mackay

                  Something like this:

                  DataTable table = theDataSet.Tables[0];
                  foreach(Row aRow in table.Rows)
                  {
                  string Url = aRow[1];
                  string SavePath = aRow[2];
                  }

                  You can also use the name of the column instead of the ordinal position. Does this help?


                  "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

                  K Offline
                  K Offline
                  kloepper
                  wrote on last edited by
                  #8

                  Roman Doug and Colin Thanks to all of you. This is very cool and it works great. Thanks for pointing me in the right direction. Paul :-D

                  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