Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Text
Public Class Logs

    Private cnstr = mainForm.cnstr
    Private conn As New SqlConnection(cnstr)
    Private myQuery As String = String.Empty
    Private myCommand As SqlCommand
    Private adapter As SqlDataAdapter = New SqlDataAdapter()
    Private ds As New DataSet("Machines")
    Private i, limit As Integer
    Private oldMchCode As String = String.Empty
    Private addMode, editMode As Integer
    Private row As Integer
    Private partnumber, statuses, currstat As String
    Private rawData As Boolean = False
    Public serialOnly As Boolean = False
    Private status() = New String() {"WHS", "PDN", "SMT"}

    Private Sub Logs_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = mainForm
        Control.CheckForIllegalCrossThreadCalls = False
        Me.Top = 0
        Me.Left = 0
    End Sub

    Private Sub initializeDG_inventory()
        Dim serialnum As New DataGridViewTextBoxColumn
        Dim status As New DataGridViewTextBoxColumn
        Dim partno As New DataGridViewTextBoxColumn
        Dim lastupdatedby As New DataGridViewTextBoxColumn
        Dim lastupdate As New DataGridViewTextBoxColumn
        Dim maker As New DataGridViewTextBoxColumn
        Dim lot As New DataGridViewTextBoxColumn
        Dim quantity As New DataGridViewTextBoxColumn
        Dim model As New DataGridViewTextBoxColumn
        Dim line As New DataGridViewTextBoxColumn
        Dim received As New DataGridViewTextBoxColumn

        serialnum.HeaderText = "Serial Num"
        status.HeaderText = "Status"
        partno.HeaderText = "Part Number"
        lastupdatedby.HeaderText = "User"
        lastupdate.HeaderText = "Date Created/Issued"
        maker.HeaderText = "Maker"
        lot.HeaderText = "Lot"
        quantity.HeaderText = "Quantity"
        model.HeaderText = "Model"
        line.HeaderText = "Line"
        received.HeaderText = "Received"

        dg_inventory.Columns.Add(serialnum)
        dg_inventory.Columns.Add(status)
        dg_inventory.Columns.Add(partno)
        dg_inventory.Columns.Add(lastupdatedby)
        dg_inventory.Columns.Add(lastupdate)
        dg_inventory.Columns.Add(maker)
        dg_inventory.Columns.Add(lot)
        dg_inventory.Columns.Add(quantity)
        dg_inventory.Columns.Add(model)
        dg_inventory.Columns.Add(line)
        dg_inventory.Columns.Add(received)

        dg_inventory.Columns(0).ReadOnly = True
        dg_inventory.Columns(1).ReadOnly = True
        dg_inventory.Columns(2).ReadOnly = True
        dg_inventory.Columns(3).ReadOnly = True
        dg_inventory.Columns(4).ReadOnly = True
        dg_inventory.Columns(5).ReadOnly = True
        dg_inventory.Columns(6).ReadOnly = True
        dg_inventory.Columns(7).ReadOnly = True
        dg_inventory.Columns(8).ReadOnly = True
        dg_inventory.Columns(9).ReadOnly = True
        dg_inventory.Columns(10).ReadOnly = True

        dg_inventory.Columns(0).Width = 150
        dg_inventory.Columns(1).Width = 150
        dg_inventory.Columns(2).Width = 150
        dg_inventory.Columns(3).Width = 150
        dg_inventory.Columns(4).Width = 150
        dg_inventory.Columns(5).Width = 150
        dg_inventory.Columns(6).Width = 150
        dg_inventory.Columns(7).Width = 150
        dg_inventory.Columns(8).Width = 150
        dg_inventory.Columns(9).Width = 150
        dg_inventory.Columns(10).Width = 150

        dg_inventory.SelectionMode = DataGridViewSelectionMode.FullRowSelect

        dg_inventory.Columns(0).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_inventory.Columns(1).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_inventory.Columns(2).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_inventory.Columns(3).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_inventory.Columns(4).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_inventory.Columns(5).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_inventory.Columns(6).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_inventory.Columns(7).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_inventory.Columns(8).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_inventory.Columns(9).SortMode = DataGridViewColumnSortMode.Programmatic
        dg_inventory.Columns(10).SortMode = DataGridViewColumnSortMode.Programmatic
    End Sub

    Private Sub fillDataSet_inventory()
        Dim conn As New SqlConnection(cnstr)

        myQuery = "SELECT lot_no,status,part_no,lastupdatedby,lastupdate,maker,lot,quantity,model,line,received FROM [dbo].[PartsPrepLog]  WHERE [lot_no] LIKE '%" & txtSearch.Text & "%' OR [part_no] LIKE '%" & txtSearch.Text & "%'"

        myCommand = New SqlCommand(myQuery, conn)

        Try
            conn.Open()
            adapter.SelectCommand = myCommand
            ds.Clear()
            adapter.Fill(ds)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            conn.Close()
            conn.Dispose()
        End Try
    End Sub

    Private Sub fillDataGridView_inventory()
        Dim i As Integer = 0

        dg_inventory.Rows.Clear()
        For i = 0 To ds.Tables(0).Rows.Count - 1
            row = dg_inventory.Rows.Add()
            dg_inventory.Rows(row).Cells(0).Value = ds.Tables(0).Rows(row).Item("lot_no")
            dg_inventory.Rows(row).Cells(1).Value = ds.Tables(0).Rows(row).Item("status")
            dg_inventory.Rows(row).Cells(2).Value = ds.Tables(0).Rows(row).Item("part_no")
            dg_inventory.Rows(row).Cells(3).Value = ds.Tables(0).Rows(row).Item("lastupdatedby")
            dg_inventory.Rows(row).Cells(4).Value = ds.Tables(0).Rows(row).Item("lastupdate")
            dg_inventory.Rows(row).Cells(5).Value = ds.Tables(0).Rows(row).Item("maker")
            dg_inventory.Rows(row).Cells(6).Value = ds.Tables(0).Rows(row).Item("lot")
            dg_inventory.Rows(row).Cells(7).Value = ds.Tables(0).Rows(row).Item("quantity")
            dg_inventory.Rows(row).Cells(8).Value = ds.Tables(0).Rows(row).Item("model")
            dg_inventory.Rows(row).Cells(9).Value = ds.Tables(0).Rows(row).Item("line")
            dg_inventory.Rows(row).Cells(10).Value = ds.Tables(0).Rows(row).Item("received")

        Next
    End Sub

    Private Sub txtSearch_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtSearch.KeyPress
        If Asc(e.KeyChar) = 13 Then
            dg_inventory.Columns.Clear()
            initializeDG_inventory()
            fillDataSet_inventory()
            fillDataGridView_inventory()
        End If
    End Sub
End Class