Functions returns a value/statement/datatable,etc when it is being triggered. This is a sample of function that returns a datatable :
Public Function Get_records() As Data.DataTable
Try
Dim sSQL As String = "SELECT * FROM tenant_management.tbluser WHERE active = 'Y'"
Dim myDA As New MySqlDataAdapter(sSQL, myCon)
Dim dsData As New DataSet()
myDA.Fill(dsData, "Data")
Return dsData.Tables("Data")
myCon.Close()
Catch ex As Exception
myCon.Close()
Throw
Finally
myCon.Close()
End Try
End Function
Try
Dim sSQL As String = "SELECT * FROM tenant_management.tbluser WHERE active = 'Y'"
Dim myDA As New MySqlDataAdapter(sSQL, myCon)
Dim dsData As New DataSet()
myDA.Fill(dsData, "Data")
Return dsData.Tables("Data")
myCon.Close()
Catch ex As Exception
myCon.Close()
Throw
Finally
myCon.Close()
End Try
End Function
This can be used in a class or in your winform.
"mycon" in this function is my connection which should be assigned a connectionstring.
To call this function, you should assign your grid's datasource property to this function which returns a datatable.
If you are using a class, the syntax of assigning the datasource should be like this:
Dim getClass As New Classname()
grd.DataSource = getClass.Get_records
grd.DataSource = getClass.Get_records
if you are not using a class and you just use the function in the winform, the syntax should be like this:
datagrid.DataSource = Get_records
datagrid.DataSource = Get_records
The bottomline is: you can use your function anytime you want. You just simply call the function instead of coding again the whole content of the function.