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:

Dim openFileDialog1 As New OpenFileDialog()
        openFileDialog1.Filter = "All Files..|*.*"
        openFileDialog1.Title = "Open File..."
        Dim J As Integer = 0
        Dim C As Integer = 0
        Dim K As Integer = 0
        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            Dim loadscr As txtload
            loadscr = New txtload
            Dim wait As Timer
            wait = New Timer
            wait.Interval = 1000

                Dim fs As New IO.FileStream(openFileDialog1.FileName, IO.FileMode.Open)
                Dim sr As New IO.StreamReader(fs)

                Dim x() As String = sr.ReadLine().Split(CType(",", Char))
                Dim NoCol As Integer = x.Length
                Dim TxtTable As DataTable
                TxtTable = New DataTable
                Dim TxtRow As DataRow
                'TxtRow = TxtTable.NewRow()
                While C < x.Length
                    C = C + 1
                End While
                filedrop.SelectedIndex = 0
                Dim r() As String = sr.ReadToEnd().Split(CType(vbCrLf, Char))
                Dim colNo As Integer = TxtTable.Columns.Count()

                While J < r.Length - 1
                    Dim col() As String = r(J).Split(CType(",", Char))
                    TxtRow = TxtTable.NewRow()
                    K = 0
                    While K < colNo
                        TxtRow.Item(K) = col(K)
                        K = K + 1
                    End While


                    J = J + 1
                End While

                Grid1.DataSource = TxtTable


                MsgBox("Exception File Loaded", MsgBoxStyle.OkOnly = 0, "Exception Loaded")
            Catch ex As Exception
            End Try

        End If
8 Years
Discussion Span
Last Post by cellus205

you can pass a datatable just like any other class (string, integer, object, etc). What do you mean by another function?


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?


What you could do is declare the datatable as private and then you would be able to access it from the button function.

Votes + Comments
Thanks a lot

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

Public Class Form1
Private dtTable1 as Datatable
Private Sub LoadDataTable
   dtTable = new DataTable
   dtTable = FillDataTableCode
End Sub

Private Sub ButtonClick
   grd1.datasource = dtTable
end Sub

End Class

Hope that helps! Good luck!

Votes + Comments
Thanks a lot, exactly what I needed

Ohh ok I get it, thanks a lot guys, I knew it was something simple I was just looking over

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.