943,866 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 3663
  • VB.NET RSS
Aug 20th, 2008
0

Passing a DataTable VB .NET 08

Expand Post »
Hows it going everyone. Im pretty sure theres a simple way to do this, but cant figure it out right now. Im still relatively new to vb .net, and have a function that reads a csv file into a datatable and fills a datagrid. But what I want to do is use this datatable to fill a grid in a different function. So what would be the easiest way to pass a filled datatable to a new function?

Heres the code:

VB Syntax (Toggle Plain Text)
  1. Dim openFileDialog1 As New OpenFileDialog()
  2. openFileDialog1.Filter = "All Files..|*.*"
  3. openFileDialog1.Title = "Open File..."
  4. Dim J As Integer = 0
  5. Dim C As Integer = 0
  6. Dim K As Integer = 0
  7. If openFileDialog1.ShowDialog() = DialogResult.OK Then
  8. Dim loadscr As txtload
  9. loadscr = New txtload
  10. loadscr.Show()
  11. Dim wait As Timer
  12. wait = New Timer
  13. wait.Interval = 1000
  14. wait.Start()
  15. wait.Stop()
  16.  
  17. Try
  18. Dim fs As New IO.FileStream(openFileDialog1.FileName, IO.FileMode.Open)
  19. Dim sr As New IO.StreamReader(fs)
  20.  
  21. Dim x() As String = sr.ReadLine().Split(CType(",", Char))
  22. Dim NoCol As Integer = x.Length
  23. Dim TxtTable As DataTable
  24. TxtTable = New DataTable
  25. Dim TxtRow As DataRow
  26. 'TxtRow = TxtTable.NewRow()
  27. While C < x.Length
  28. TxtTable.Columns.Add(x(C))
  29. filedrop.Items.Add(x(C))
  30. C = C + 1
  31. End While
  32. filedrop.SelectedIndex = 0
  33. Dim r() As String = sr.ReadToEnd().Split(CType(vbCrLf, Char))
  34. Dim colNo As Integer = TxtTable.Columns.Count()
  35.  
  36. While J < r.Length - 1
  37. Dim col() As String = r(J).Split(CType(",", Char))
  38. TxtRow = TxtTable.NewRow()
  39. K = 0
  40. While K < colNo
  41. TxtRow.Item(K) = col(K)
  42. K = K + 1
  43. End While
  44.  
  45. TxtTable.Rows.Add(TxtRow)
  46. TxtRow.AcceptChanges()
  47.  
  48. J = J + 1
  49. End While
  50.  
  51. Grid1.DataSource = TxtTable
  52. 'Table1TableAdapter.Fill(TestdbDataSet.Table1)
  53.  
  54. loadscr.Close()
  55.  
  56. MsgBox("Exception File Loaded", MsgBoxStyle.OkOnly = 0, "Exception Loaded")
  57. Catch ex As Exception
  58. System.Windows.Forms.MessageBox.Show(ex.Message)
  59. End Try
  60.  
  61. End If
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
cellus205 is offline Offline
57 posts
since May 2007
Sep 3rd, 2008
0

Re: Passing a DataTable VB .NET 08

any1 able to help with this?
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
cellus205 is offline Offline
57 posts
since May 2007
Sep 3rd, 2008
0

Re: Passing a DataTable VB .NET 08

you can pass a datatable just like any other class (string, integer, object, etc). What do you mean by another function?
Reputation Points: 12
Solved Threads: 2
Newbie Poster
asnider is offline Offline
23 posts
since Jul 2008
Sep 3rd, 2008
0

Re: Passing a DataTable VB .NET 08

I mean like since I have filled the datatable in this sub function, and I need the information from the datatable to fill a grid later in a button_click function, how would I pass this filled datatable to the button_click to fill the grid?
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
cellus205 is offline Offline
57 posts
since May 2007
Sep 3rd, 2008
1

Re: Passing a DataTable VB .NET 08

What you could do is declare the datatable as private and then you would be able to access it from the button function.
Last edited by ThatOtherGuy; Sep 3rd, 2008 at 2:49 pm.
Reputation Points: 12
Solved Threads: 1
Newbie Poster
ThatOtherGuy is offline Offline
4 posts
since Sep 2008
Sep 3rd, 2008
1

Re: Passing a DataTable VB .NET 08

Yeah, you would declare it as a form level variable....for instance:

VB.NET Syntax (Toggle Plain Text)
  1. Public Class Form1
  2. Private dtTable1 as Datatable
  3. Private Sub LoadDataTable
  4. dtTable = new DataTable
  5. dtTable = FillDataTableCode
  6. End Sub
  7.  
  8. Private Sub ButtonClick
  9. grd1.datasource = dtTable
  10. end Sub
  11.  
  12.  
  13. End Class

Hope that helps! Good luck!
Reputation Points: 12
Solved Threads: 2
Newbie Poster
asnider is offline Offline
23 posts
since Jul 2008
Sep 4th, 2008
0

Re: Passing a DataTable VB .NET 08

Ohh ok I get it, thanks a lot guys, I knew it was something simple I was just looking over
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
cellus205 is offline Offline
57 posts
since May 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Insert data for mobile 5.0(vs 2008)
Next Thread in VB.NET Forum Timeline: Saving Drawn Bitmaps





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC