Gridview / Datatable - extract as String Variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tonymcq
    New Member
    • Jul 2008
    • 2

    Gridview / Datatable - extract as String Variable

    I am very new to VB.net / ASP and have spent hours and hours trying to solve what must be a simple problem - hopefully someone can point me in the right direction.

    I have a gridview bound to an MS Access database. A user selects an item which is then added to a datatable, contents appear in a second gridview at the bottom of the page. (Yes it's a simple Shopping Cart) - there are four fields - ID, Product, Quantity, Cost. When the user has finished adding items I want to capture the contents of the second gridview (or the datatable) and transfer it to a multi line textbox on another page, which can then be printed or emailed. : Session("print" ) = xxxxx Server.Transfer ("~/printorder.aspx ")

    Searching the net has found two export options (Export to Excel / Export to CSV) both these work (HTMLTextWriter ) but save a file to disk which doesn't help.

    Can anyone explain the best way of extracting the four fields in the datatable / gridview as strings, then adding / appending them to a variable. What is the best way to handle this - a loop, a streamwriter or any other options ??

    Any VB examples to get me started would be much appreciated .... even C (I have found translation sites)

    Thanks in advance.
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Well, if you are adding rows to a DataTable, then you can easily loop through that DataTable and get at your values.

    I'm not sure what your end goal is, so I just made an example where you add all values to one long string. But you can see what you need to do:
    Code:
    Dim table As New DataTable() 'just so I have a table to play with
    Dim row As DataRow
    Dim longstring As String
    longstring = ""
    For Each row In table.Rows
        longstring = longstring & row(0)
        longstring = longstring & row(1)
        'and so forth
        'or you can use the column name:
        longstring = longstring & row("colName")
    Next

    Comment

    • tonymcq
      New Member
      • Jul 2008
      • 2

      #3
      You got me moving in the right direction - I can now extract the contents from the datatable and transfer them to another ASP page.
      Thank-you for your help.

      Comment

      Working...