VB.Net - Using Data Table Load Data Into Data Grid. This Code For VB.net Beginners This Is Only A O
VB.Net - Using Data Table Load Data Into Data Grid. This Code For VB.net Beginners This Is Only A One Method, There Have Another Methods Also
API Declarations
Imports System.Data
Imports System.Data.SqlClient
Rate VB.Net - Using Data Table Load Data Into Data Grid. This Code For VB.net Beginners This Is Only A O
(2(2 Vote))
Private OleCon As System.Data.OleDb.OleDbConnection
Private OleCom As System.Data.OleDb.OleDbCommand
Private OleAdapter As System.Data.OleDb.OleDbDataAdapter
Private OleDataSet As System.Data.DataSet
Private DTable As System.Data.DataTable
Private DRow As System.Data.DataRow
Private DCol As System.Data.DataColumn
Private Sub CmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdLoad.Click
Try
Me.Cursor = Cursors.WaitCursor
LoadIntoAdapter()
DTable = New System.Data.DataTable("Products")
DTable = LoadData()
MessageBox.Show("Data Loaded Successfully", "OLEDB Connection", MessageBoxButtons.OK, MessageBoxIcon.Information)
CmdLoad.Enabled = False
CmdSend.Enabled = True
Me.Cursor = Cursors.Default
Catch ex As Exception
MessageBox.Show(ex.Message, "Error ...", MessageBoxButtons.OK, MessageBoxIcon.Error)
Me.Cursor = Cursors.Default
End Try
End Sub
Private Sub FrmDataTable_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Me.Cursor = Cursors.WaitCursor
OleCon = New Data.OleDb.OleDbConnection
OleCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Program Files\Microsoft Visual Studio\VB98\NWIND.MDB"
OleCon.Open()
CmdLoad.Enabled = True
CmdSend.Enabled = False
DataGrid.Enabled = False
Me.Cursor = Cursors.Default
Catch ex As Exception
MessageBox.Show(ex.Message, "Error ...", MessageBoxButtons.OK, MessageBoxIcon.Error)
CmdLoad.Enabled = False
CmdSend.Enabled = False
DataGrid.Enabled = False
Me.Cursor = Cursors.Default
End Try
End Sub
Private Function LoadData() As System.Data.DataTable
Try
OleDataSet = New System.Data.DataSet()
OleAdapter.Fill(OleDataSet, "Products")
Return OleDataSet.Tables("Products")
Catch ex As Exception
Throw ex
End Try
End Function
Private Sub LoadIntoAdapter()
Try
OleAdapter = New System.Data.OleDb.OleDbDataAdapter()
OleCom = New Data.OleDb.OleDbCommand("SELECT * FROM Products ORDER BY ProductID", OleCon)
OleAdapter.SelectCommand = OleCom
Catch ex As Exception
Throw ex
End Try
End Sub
Private Sub CmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSend.Click
DataGrid.Enabled = True
Me.Cursor = Cursors.WaitCursor
'Adding Coloumn To Grid
For Each DCol In DTable.Columns
DataGrid.Columns.Add(DCol.ColumnName.ToString, DCol.Caption.ToString())
Next
'Adding Rows To Grid
For Each DRow In DTable.Rows
DataGrid.Rows.Add()
For Each DCol In DTable.Columns()
DataGrid.Rows(DataGrid.Rows.Count - 1).Cells(DCol.ColumnName.ToString()).Value = _
DRow.Item(DCol.ColumnName.ToString()).ToString()
Next DCol
Application.DoEvents()
Next
Me.Cursor = Cursors.Default
End Sub
VB.Net - Using Data Table Load Data Into Data Grid. This Code For VB.net Beginners This Is Only A O Comments
No comments yet — be the first to post one!
Post a Comment