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. How can I build a String by reading through a Data Grid?

How can I build a String by reading through a Data Grid?

Scheduled Pinned Locked Moved Visual Basic
helpquestioncsharpcss
4 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.
  • C Offline
    C Offline
    CCG3
    wrote on last edited by
    #1

    Hello all, I need a little help on how to do something. I am working with VB.Net 2005 and I need to read through each row on a Data Grid and grab the values of each field and save it to a formatted String so I can pass that String over to a Crystal Report as a parameter. I can handle passing the String to CR but building it from the Data Grid is where I am getting stuck. I need for it look like this…. Row1Value1(tab) Row1Value2(tab) Row1Value3 Row2Value1(tab) Row2Value2(tab) Row2Value3 I really appreciate any help/suggestions that anyone can provide.

    D T 2 Replies Last reply
    0
    • C CCG3

      Hello all, I need a little help on how to do something. I am working with VB.Net 2005 and I need to read through each row on a Data Grid and grab the values of each field and save it to a formatted String so I can pass that String over to a Crystal Report as a parameter. I can handle passing the String to CR but building it from the Data Grid is where I am getting stuck. I need for it look like this…. Row1Value1(tab) Row1Value2(tab) Row1Value3 Row2Value1(tab) Row2Value2(tab) Row2Value3 I really appreciate any help/suggestions that anyone can provide.

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

      I can see this getting ugly very fast... All you have to do is iterate over the collection of rows in the DataGridView. And in each Row, iterate over the collection of cells:

      For Each r As DataGridViewRow in DataGridView1.Rows
      For Each c As DataGridViewCell in r
      Dim value As String = c.Value.ToString()
      Next
      Next

      You'll, of course, have to modify this for your own situation. It won't concatenate a string together. It just shows you have to get at the values.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      C 1 Reply Last reply
      0
      • D Dave Kreskowiak

        I can see this getting ugly very fast... All you have to do is iterate over the collection of rows in the DataGridView. And in each Row, iterate over the collection of cells:

        For Each r As DataGridViewRow in DataGridView1.Rows
        For Each c As DataGridViewCell in r
        Dim value As String = c.Value.ToString()
        Next
        Next

        You'll, of course, have to modify this for your own situation. It won't concatenate a string together. It just shows you have to get at the values.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        C Offline
        C Offline
        CCG3
        wrote on last edited by
        #3

        Thanks Dave, I will see take this example and see what I can come up with over the weekend. As always, you are a great help!! Have a great weekend!

        1 Reply Last reply
        0
        • C CCG3

          Hello all, I need a little help on how to do something. I am working with VB.Net 2005 and I need to read through each row on a Data Grid and grab the values of each field and save it to a formatted String so I can pass that String over to a Crystal Report as a parameter. I can handle passing the String to CR but building it from the Data Grid is where I am getting stuck. I need for it look like this…. Row1Value1(tab) Row1Value2(tab) Row1Value3 Row2Value1(tab) Row2Value2(tab) Row2Value3 I really appreciate any help/suggestions that anyone can provide.

          T Offline
          T Offline
          Trupti Mehta
          wrote on last edited by
          #4

          Hi, To set a String with tabs, try the following code. Its a modified version of what Dave provided. Dim mainStr as String = "" Dim rowStr as String = "" For Each r As DataGridViewRow in DataGridView1.Rows rowStr = "" For Each c As DataGridViewCell in r If (c.ColumnIndex > 0) Then rowStr += ControlChars.Tab End If rowStr += c.Value.ToString() Next mainStr += rowStr + ControlChars.CrLf Next This code will produce the results as u wanted i.e.

          CCG3 wrote:

          Row1Value1(tab) Row1Value2(tab) Row1Value3 Row2Value1(tab) Row2Value2(tab) Row2Value3

          Hope this helps to your expectations.

          Thanks Terry

          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