Passing a DataTable VB .NET 08

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2007
Posts: 45
Reputation: cellus205 is an unknown quantity at this point 
Solved Threads: 1
cellus205 cellus205 is offline Offline
Light Poster

Passing a DataTable VB .NET 08

 
0
  #1
Aug 20th, 2008
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:

  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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 45
Reputation: cellus205 is an unknown quantity at this point 
Solved Threads: 1
cellus205 cellus205 is offline Offline
Light Poster

Re: Passing a DataTable VB .NET 08

 
0
  #2
Sep 3rd, 2008
any1 able to help with this?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 23
Reputation: asnider is an unknown quantity at this point 
Solved Threads: 2
asnider asnider is offline Offline
Newbie Poster

Re: Passing a DataTable VB .NET 08

 
0
  #3
Sep 3rd, 2008
you can pass a datatable just like any other class (string, integer, object, etc). What do you mean by another function?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 45
Reputation: cellus205 is an unknown quantity at this point 
Solved Threads: 1
cellus205 cellus205 is offline Offline
Light Poster

Re: Passing a DataTable VB .NET 08

 
0
  #4
Sep 3rd, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 4
Reputation: ThatOtherGuy is an unknown quantity at this point 
Solved Threads: 1
ThatOtherGuy ThatOtherGuy is offline Offline
Newbie Poster

Re: Passing a DataTable VB .NET 08

 
1
  #5
Sep 3rd, 2008
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.
Only the dead see the end of war...
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 23
Reputation: asnider is an unknown quantity at this point 
Solved Threads: 2
asnider asnider is offline Offline
Newbie Poster

Re: Passing a DataTable VB .NET 08

 
1
  #6
Sep 3rd, 2008
Yeah, you would declare it as a form level variable....for instance:

  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!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 45
Reputation: cellus205 is an unknown quantity at this point 
Solved Threads: 1
cellus205 cellus205 is offline Offline
Light Poster

Re: Passing a DataTable VB .NET 08

 
0
  #7
Sep 4th, 2008
Ohh ok I get it, thanks a lot guys, I knew it was something simple I was just looking over
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC