Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Text Public Class PlantMaintenanceForm Private cnstr = mainForm.cnstr Dim ds As New DataSet("Plant") Dim adapter As SqlDataAdapter = New SqlDataAdapter Dim myQuery As String Dim myCommand As SqlCommand Private i, j, limit, selectedRow, selectedIndex, selectedColumn As Integer Private oldPlant As String = String.Empty Private Plant As New Plant Private addmode, editmode, filtermode As Integer Private connection As Connection Private Sub PlantMaintenanceForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.MdiParent = mainForm txtplant.Enabled = False disableControls() toNormalMode() addmode = 0 editmode = 0 selectedRow = -1 selectedColumn = -1 initializedDG() fillDataSet() fillDataGridView() moveFirst() End Sub Private Sub enableControls() txtplant.Enabled = True End Sub Private Sub disableControls() txtplant.Enabled = False End Sub Private Sub clear() txtplant.Text = String.Empty End Sub Private Sub toNormalMode() ts_first.Enabled = True ts_prev.Enabled = True ts_next.Enabled = True ts_last.Enabled = True 'ts_print.Enabled = True ts_new.Enabled = True ts_edit.Enabled = True ts_delete.Enabled = True 'ts_find.Enabled = True ts_revert.Enabled = False ts_save.Enabled = False dg.Enabled = True disableControls() End Sub Public Sub toEditMode() ts_first.Enabled = False ts_prev.Enabled = False ts_next.Enabled = False ts_last.Enabled = False 'ts_print.Enabled = False ts_new.Enabled = False ts_edit.Enabled = False ts_delete.Enabled = False 'ts_find.Enabled = False ts_revert.Enabled = True ts_save.Enabled = True dg.Enabled = False enableControls() End Sub Public Sub toAddMode() ts_first.Enabled = False ts_prev.Enabled = False ts_next.Enabled = False ts_last.Enabled = False '.Enabled = False ts_new.Enabled = False ts_edit.Enabled = False ts_delete.Enabled = False 'ts_find.Enabled = False ts_revert.Enabled = True ts_save.Enabled = True dg.Enabled = False enableControls() End Sub Private Sub initializedDG() Dim plantCode As New DataGridViewTextBoxColumn Dim createddate As New DataGridViewTextBoxColumn plantCode.HeaderText = "PlantCode" createddate.HeaderText = "Created Date" dg.Columns.Add(plantCode) dg.Columns.Add(createddate) dg.Columns(0).Width = 150 dg.Columns(1).Width = 150 dg.Columns(0).ReadOnly = True dg.Columns(1).ReadOnly = True dg.Columns(0).SortMode = DataGridViewColumnSortMode.Programmatic dg.Columns(1).SortMode = DataGridViewColumnSortMode.Programmatic End Sub Private Sub fillDataSet() Dim conn As New SqlConnection(cnstr) myQuery = "SELECT plantCode, createddate FROM PLANTS" myCommand = New SqlCommand(myQuery, conn) Try conn.Open() adapter.SelectCommand = myCommand ds.Clear() adapter.Fill(ds) i = 0 limit = ds.Tables(0).Rows.Count - 1 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() Dim i As Integer = 0 dg.Rows.Clear() For i = 0 To ds.Tables(0).Rows.Count - 1 dg.Rows.Add() dg.Rows(i).Cells(0).Value = ds.Tables(0).Rows(i).Item("plantCode") dg.Rows(i).Cells(1).Value = ds.Tables(0).Rows(i).Item("createddate") Next End Sub Public Sub moveFirst() If ds.Tables(0).Rows.Count <> 0 And limit > -1 Then i = 0 BindingNavigatorPositionItem.Text = CStr(i + 1) BindingNavigatorCountItem.Text = "of " & CStr(limit + 1) txtplant.Text = ds.Tables(0).Rows(i).Item("plantCode") dg.SelectionMode = DataGridViewSelectionMode.FullRowSelect dg.ClearSelection() dg.Rows(i).Selected = True Else clear() End If End Sub Public Sub moveNext() If i < limit And limit > -1 Then i = i + 1 BindingNavigatorPositionItem.Text = CStr(i + 1) BindingNavigatorCountItem.Text = "of " & CStr(limit + 1) txtplant.Text = ds.Tables(0).Rows(i).Item("plantCode") dg.SelectionMode = DataGridViewSelectionMode.FullRowSelect dg.ClearSelection() dg.Rows(i).Selected = True End If End Sub Public Sub movePrevious() If i <> 0 And limit > -1 Then i = i - 1 BindingNavigatorPositionItem.Text = CStr(i + 1) BindingNavigatorCountItem.Text = "of " & CStr(limit + 1) txtplant.Text = ds.Tables(0).Rows(i).Item("plantCode") dg.SelectionMode = DataGridViewSelectionMode.FullRowSelect dg.ClearSelection() dg.Rows(i).Selected = True End If End Sub Public Sub moveLast() If limit > -1 Then i = limit BindingNavigatorPositionItem.Text = CStr(i + 1) BindingNavigatorCountItem.Text = "of " & CStr(limit + 1) txtplant.Text = ds.Tables(0).Rows(i).Item("plantCode") dg.SelectionMode = DataGridViewSelectionMode.FullRowSelect dg.ClearSelection() dg.Rows(i).Selected = True Else clear() End If End Sub Public Sub moveActual() If limit > -1 Then BindingNavigatorPositionItem.Text = CStr(i + 1) BindingNavigatorCountItem.Text = "of " & CStr(limit + 1) txtplant.Text = ds.Tables(0).Rows(i).Item("plantCode") dg.SelectionMode = DataGridViewSelectionMode.FullRowSelect dg.ClearSelection() dg.Rows(i).Selected = True Else clear() End If End Sub Public Sub findLocation(ByVal mmcode As String) Dim counter As Integer = 0 While counter < ds.Tables(0).Rows.Count If mmcode = ds.Tables(0).Rows(counter).Item("plantCode") Then i = counter moveActual() Exit While End If counter = counter + 1 End While End Sub Private Sub dg_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dg.CellClick Try findLocation(dg.Rows(e.RowIndex).Cells(0).Value) Catch ex As Exception End Try End Sub Private Sub ts_new_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_new.Click addmode = 1 editmode = 0 clear() toAddMode() End Sub Private Sub ts_delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_delete.Click If MsgBox("Are you sure you want to delete plant: " & txtplant.Text & "?", MsgBoxStyle.YesNoCancel) = MsgBoxResult.Yes Then Plant.deleteRecordPlant(txtplant.Text) disableControls() fillDataSet() fillDataGridView() moveFirst() End If End Sub Private Sub ts_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_save.Click If txtplant.Text <> String.Empty Then Plant.plantCode = txtplant.Text ' Plant.createdby = loginForm.Users.userid Plant.createddate = DateTime.Now If addmode Then Plant.AddRecordPlant() disableControls() toNormalMode() fillDataSet() fillDataGridView() moveLast() ElseIf editmode Then Plant.UpdateRecordPlant(oldPlant) disableControls() toNormalMode() fillDataSet() fillDataGridView() moveLast() End If Else MessageBox.Show("Please input values before saving.", "Add Plant", MessageBoxButtons.OK, MessageBoxIcon.Warning) txtplant.Focus() End If End Sub Private Sub ts_edit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_edit.Click oldPlant = txtplant.Text editmode = 1 addmode = 0 enableControls() toEditMode() End Sub Private Sub ts_revert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_revert.Click moveActual() disableControls() toNormalMode() End Sub Private Sub ts_first_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_first.Click moveFirst() End Sub Private Sub ts_prev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_prev.Click movePrevious() End Sub Private Sub ts_next_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_next.Click moveNext() End Sub Private Sub ts_last_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ts_last.Click moveLast() End Sub End Class