May i ask how i can use code call out my data table and post on data grid view

  • Thread starter Thread starter htcting
  • Start date Start date
H

htcting

Guest
how i can write the code let my datagridview (dgvdata) can show the data table when i run the code. in current project status is when i run the project the datagridview (dgvdata) will no show the database .but when i search the data on database the database still can show...can anyone pls help take a look...thanks a lot.

Imports System.Data.OleDb
Public Class Form2
Dim cnn As New OleDb.OleDbConnection
Private dbDataSet As New DataTable
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cnn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=mydataX.mdb;"
End Sub
Private Sub RefreshData()
If Not cnn.State = ConnectionState.Open Then
'open connection
cnn.Open()
End If
Dim da As New OleDb.OleDbDataAdapter("SELECT cid, cbcid as [ID], " & _
"CheckerName as [CheckerName], Quantity as [StockQuantity],Unit as [Unit], Date as [Date], CompanyName as [CompanyName], CheckerDetail as [CheckerDetail]" & _
" FROM checker ORDER BY cid", cnn)
Dim dt As New DataTable
'fill data to datatable
da.Fill(dt)
Me.dgvData.DataSource = dt
Me.dgvData.Columns("cid").Visible = False
'close connection
cnn.Close()
End Sub

Private Sub loadTransactions(ByVal transactionid As Integer)
If Not cnn.State = ConnectionState.Open Then
'open connection
cnn.Open()
End If
Dim da As New OleDb.OleDbDataAdapter("SELECT s.cid as [ID], c.CheckerName as item_name, " & _
"s.CheckerName as [CheckerName],s.ShipQuantity as [StockQuantity], s.[Date] as [Date], s.CompanyName as [CompanyName], s.CheckerDetail as [CheckerDetail]" & _
"FROM stock s left join checker c on c.cid = s.checkercid where s.checkercid = " & transactionid & " ORDER BY s.cid", cnn)
Dim dt As New DataTable
da.Fill(dt)
Me.DataGridView1.DataSource = dt
Me.DataGridView1.Columns("item_name").HeaderText = "CheckerName"
'close connection
cnn.Close()
End Sub

Private Sub txtbarcode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtbarcode.TextChanged
cnn.Open()
Dim dt As New DataTable
Dim ds As New DataSet
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter

da = New OleDbDataAdapter("SELECT * FROM checker WHERE cbcid like '%" & txtbarcode.Text & "%'", cnn)

da.Fill(dt)

dgvData.DataSource = dt.DefaultView
cnn.Close()
End Sub

Private Sub dgvData_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvData.CellEnter
If dgvData.SelectedCells.Count > 0 Then
loadTransactions(dgvData("cid", dgvData.CurrentCell.RowIndex).Value)
End If
End Sub

Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
Me.Close()
End Sub

Private Sub dgvData_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvData.CellFormatting
Dim dgv As DataGridView = Me.dgvData
For i As Integer = 0 To dgv.Rows.Count - 1
For ColNo As Integer = 1 To 7
If Not dgv.Rows(i).Cells(ColNo).Value Is DBNull.Value Then
dgv.Rows(i).Cells(ColNo).Style.BackColor = Color.LightCyan
End If
Next
Next

For i As Integer = 0 To Me.dgvData.Rows.Count - 1
If Not IsDBNull(Me.dgvData.Rows(i).Cells(3).Value) Then
If Me.dgvData.Rows(i).Cells(3).Value = "0" Then
Me.dgvData.Rows(i).DefaultCellStyle.ForeColor = Color.Red
End If
Else

End If

Next
End Sub
End Class

Continue reading...
 
Back
Top