Skip to content

Instantly share code, notes, and snippets.

@facebookegypt
Created July 3, 2020 13:28
Show Gist options
  • Save facebookegypt/d900d743d5f7bf1db3388a6796b8913f to your computer and use it in GitHub Desktop.
Save facebookegypt/d900d743d5f7bf1db3388a6796b8913f to your computer and use it in GitHub Desktop.
'Visual Basi .Net Class
Imports System.Data.OleDb
Public Class FillDataGridView
Public Shared Function FillDataGridFromTable(TableName As String) As DataTable
Dim SqlStr As String = ("SELECT * FROM [" & TableName & "]")
Dim MY_CONN_STR As String = MY_CONNECTIONSTRING_FROM_CONFIGFILES
Using MyTable As DataTable = New DataTable
Try
Using Conn As New OleDbConnection With {.ConnectionString = MY_CONN_STR},
CMD As New OleDbCommand(SqlStr, Conn)
Conn.Open()
Using MyReader As OleDbDataReader = CMD.ExecuteReader
MyTable.Load(MyReader)
End Using
End Using
Catch ex As OleDbException
MsgBox(ex.Message)
End Try
Return MyTable
End Using
End Function
End Class
'Visual Basic .Net Form
Imports System.ComponentModel
Public Class Form1
'Visual Basic Controls on Form (Button, TextBox, DataGridView)
Private Sub ButtonFill_Click(sender As Object, e As EventArgs) Handles ButtonFill.Click
Dim ThisTable As String = TableTextBox.Text
With DataGridView1
.AllowUserToAddRows = False
With .ColumnHeadersDefaultCellStyle
.Alignment = DataGridViewContentAlignment.MiddleCenter
.BackColor = Color.LightGray
.ForeColor = Color.DarkBlue
End With
.EnableHeadersVisualStyles = False
.DataSource = DatabaseSettings.FillDataGridFromTable(ThisTable)
End With
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment