VB.net Datagridview with autocomplete DataGridViewComboBoxColumn

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi to all.
Im in the middle of a small project and i want your help.
I have a Datagridview with totally 13 columns (11 visible).
The Datagridview fill from an access, and
i want the first visible column (cmb1) to be able to autofillthe combobox.
The code is little be huge because the columns 2 and 4 is date field and 3 and 5 time field where i show a mask in the user.
All the combobox as you see filling in the form load and the user will not be able to add anything.
PS If you see anything else that you think is need change please feel free to correct me.
Sorry for the huge post.
Here is the code.Imports System.Data.OleDb

Public Class Bkpdwn_D_List
Public w_Bkpdwn_H_ID As Long
Dim cmd As OleDbCommand
Dim ds As New DataSet
Dim da As OleDbDataAdapter
Dim cmb1 As New DataGridViewComboBoxColumn() Κωδικός Βλάβης
Dim cmb1value As Boolean
Dim cmb2 As New DataGridViewComboBoxColumn() Μηχανοδηγός
Dim cmb3 As New DataGridViewComboBoxColumn() Συσκευαστής 1
Dim cmb4 As New DataGridViewComboBoxColumn() Συσκευαστής 2
Dim cmb5 As New DataGridViewComboBoxColumn() Συσκευαστής 3

Private WithEvents maskedTextBox1 As MaskedTextBox
Private WithEvents maskedTextBox2 As MaskedTextBox
Private WithEvents maskedTextBox3 As MaskedTextBox
Private WithEvents maskedTextBox4 As MaskedTextBox

Private Sub Bkpdwn_D_List_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Call fill_cbos()
Call fill_header()
Call fill_dataGrid()

maskedTextBox1 = New MaskedTextBox
With maskedTextBox1
.Mask = "00/00/0000"
.Visible = False
End With

maskedTextBox2 = New MaskedTextBox
With maskedTextBox2
.Mask = "00:00"
.Visible = False
End With

maskedTextBox3 = New MaskedTextBox
With maskedTextBox3
.Mask = "00/00/0000"
.Visible = False
End With

maskedTextBox4 = New MaskedTextBox
With maskedTextBox4
.Mask = "00:00"
.Visible = False
End With

Bkpdwn_D_DataGridView.Controls.Add(maskedTextBox1)
Bkpdwn_D_DataGridView.Controls.Add(maskedTextBox2)
Bkpdwn_D_DataGridView.Controls.Add(maskedTextBox3)
Bkpdwn_D_DataGridView.Controls.Add(maskedTextBox4)
End Sub

Private Sub Bkpdwn_D_DataGridView_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles Bkpdwn_D_DataGridView.CellBeginEdit
If (e.ColumnIndex = 3) Then
Dim rect1 As Rectangle = Me.Bkpdwn_D_DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)
Me.maskedTextBox1.Size = rect1.Size
Me.maskedTextBox1.Location = rect1.Location
If (Me.Bkpdwn_D_DataGridView.CurrentCell.Value IsNot Nothing) Then
Me.maskedTextBox1.Text = Me.Bkpdwn_D_DataGridView.CurrentCell.FormattedValue.ToString()
End If
Me.maskedTextBox1.Visible = True
End If

If (e.ColumnIndex = 4) Then
Dim rect2 As Rectangle = Me.Bkpdwn_D_DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)
Me.maskedTextBox2.Size = rect2.Size
Me.maskedTextBox2.Location = rect2.Location
If (Me.Bkpdwn_D_DataGridView.CurrentCell.Value IsNot Nothing) Then
Me.maskedTextBox2.Text = Me.Bkpdwn_D_DataGridView.CurrentCell.FormattedValue.ToString()
End If
Me.maskedTextBox2.Visible = True
End If

If (e.ColumnIndex = 5) Then
Dim rect3 As Rectangle = Me.Bkpdwn_D_DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)
Me.maskedTextBox3.Size = rect3.Size
Me.maskedTextBox3.Location = rect3.Location
If (Me.Bkpdwn_D_DataGridView.CurrentCell.Value IsNot Nothing) Then
Me.maskedTextBox3.Text = Me.Bkpdwn_D_DataGridView.CurrentCell.FormattedValue.ToString()
End If
Me.maskedTextBox3.Visible = True
End If

If (e.ColumnIndex = 6) Then
Dim rect4 As Rectangle = Me.Bkpdwn_D_DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)
Me.maskedTextBox4.Size = rect4.Size
Me.maskedTextBox4.Location = rect4.Location
If (Me.Bkpdwn_D_DataGridView.CurrentCell.Value IsNot Nothing) Then
Me.maskedTextBox4.Text = Me.Bkpdwn_D_DataGridView.CurrentCell.FormattedValue.ToString()
End If
Me.maskedTextBox4.Visible = True
End If
End Sub

Private Sub Bkpdwn_D_DataGridView_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles Bkpdwn_D_DataGridView.CellValidating
If (e.ColumnIndex = 3) Then
If maskedTextBox1.Visible = True Then
If maskedTextBox1.Text <> " / /" Then
If IsDate(maskedTextBox1.Text) Then
Me.Bkpdwn_D_DataGridView.CurrentCell.Value = maskedTextBox1.Text
Me.maskedTextBox1.Visible = False
Else
MsgBox("Μη αποδεκτή ημερομηνία. Η ημερομηνία πρέπει να είναι της μορφής ΗΗ/ΜΜ/ΕΕΕΕ")
maskedTextBox1.Text = ""
e.Cancel = True
End If
Else
Me.Bkpdwn_D_DataGridView.CurrentCell.Value = ""
Me.maskedTextBox1.Visible = False
End If
End If
End If

If (e.ColumnIndex = 4) Then
If maskedTextBox2.Visible = True Then
If maskedTextBox2.Text <> " :" Then
If IsDate(maskedTextBox2.Text) Then
Me.Bkpdwn_D_DataGridView.CurrentCell.Value = maskedTextBox2.Text
Me.maskedTextBox2.Visible = False
Else
MsgBox("Μη αποδεκτή ώρα. Η ώρα πρέπει να είναι της μορφής ΩΩ:ΛΛ")
maskedTextBox2.Text = ""
e.Cancel = True
End If
Else
Me.Bkpdwn_D_DataGridView.CurrentCell.Value = String.Empty
Me.maskedTextBox2.Visible = False
End If
End If
End If

If (e.ColumnIndex = 5) Then
If maskedTextBox3.Visible = True Then
If maskedTextBox3.Text <> " / /" Then
If IsDate(maskedTextBox3.Text) Then
Me.Bkpdwn_D_DataGridView.CurrentCell.Value = maskedTextBox3.Text
Me.maskedTextBox3.Visible = False
Else
MsgBox("Μη αποδεκτή ημερομηνία. Η ημερομηνία πρέπει να είναι της μορφής ΗΗ/ΜΜ/ΕΕΕΕ")
maskedTextBox3.Text = ""
e.Cancel = True
End If
Else
Me.Bkpdwn_D_DataGridView.CurrentCell.Value = ""
Me.maskedTextBox3.Visible = False
End If
End If
End If

If (e.ColumnIndex = 6) Then
If maskedTextBox4.Visible = True Then
If maskedTextBox4.Text <> " :" Then
If IsDate(maskedTextBox4.Text) Then
Me.Bkpdwn_D_DataGridView.CurrentCell.Value = maskedTextBox4.Text
Me.maskedTextBox4.Visible = False
Else
MsgBox("Μη αποδεκτή ώρα. Η ώρα πρέπει να είναι της μορφής ΩΩ:ΛΛ")
maskedTextBox4.Text = ""
e.Cancel = True
End If
Else
Me.Bkpdwn_D_DataGridView.CurrentCell.Value = String.Empty
Me.maskedTextBox4.Visible = False
End If
End If
End If
End Sub

Private IsHandleAdded1 As Boolean
Private IsHandleAdded2 As Boolean
Private IsHandleAdded3 As Boolean
Private IsHandleAdded4 As Boolean

Private Sub Bkpdwn_D_DataGridView_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles Bkpdwn_D_DataGridView.EditingControlShowing
If (Me.Bkpdwn_D_DataGridView.CurrentCell.ColumnIndex = 2) Then
cmb1value = cmb1.AutoComplete
End If


If (Not IsHandleAdded1 And Me.Bkpdwn_D_DataGridView.CurrentCell.ColumnIndex = 3) Then
Dim tx As TextBox = CType(e.Control, TextBox)
AddHandler tx.KeyPress, AddressOf Me.tx_KeyPress
IsHandleAdded1 = True
End If

If (Not IsHandleAdded2 And Me.Bkpdwn_D_DataGridView.CurrentCell.ColumnIndex = 4) Then
Dim tx As TextBox = CType(e.Control, TextBox)
AddHandler tx.KeyPress, AddressOf Me.tx_KeyPress
IsHandleAdded2 = True
End If

If (Not IsHandleAdded3 And Me.Bkpdwn_D_DataGridView.CurrentCell.ColumnIndex = 5) Then
Dim tx As TextBox = CType(e.Control, TextBox)
AddHandler tx.KeyPress, AddressOf Me.tx_KeyPress
IsHandleAdded3 = True
End If

If (Not IsHandleAdded4 And Me.Bkpdwn_D_DataGridView.CurrentCell.ColumnIndex = 6) Then
Dim tx As TextBox = CType(e.Control, TextBox)
AddHandler tx.KeyPress, AddressOf Me.tx_KeyPress
IsHandleAdded4 = True
End If
End Sub

Private Sub tx_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If (Me.Bkpdwn_D_DataGridView.CurrentCell.ColumnIndex = 3) Then
e.Handled = True
Me.maskedTextBox1.Focus()
If (Char.IsNumber(e.KeyChar)) Then
Me.maskedTextBox1.Text = e.KeyChar.ToString()
Else
Me.maskedTextBox1.SelectAll()
End If
End If

If (Me.Bkpdwn_D_DataGridView.CurrentCell.ColumnIndex = 4) Then
e.Handled = True
Me.maskedTextBox2.Focus()
If (Char.IsNumber(e.KeyChar)) Then
Me.maskedTextBox2.Text = e.KeyChar.ToString()
Else
Me.maskedTextBox2.SelectAll()
End If
End If

If (Me.Bkpdwn_D_DataGridView.CurrentCell.ColumnIndex = 5) Then
e.Handled = True
Me.maskedTextBox3.Focus()
If (Char.IsNumber(e.KeyChar)) Then
Me.maskedTextBox3.Text = e.KeyChar.ToString()
Else
Me.maskedTextBox3.SelectAll()
End If
End If

If (Me.Bkpdwn_D_DataGridView.CurrentCell.ColumnIndex = 6) Then
e.Handled = True
Me.maskedTextBox4.Focus()
If (Char.IsNumber(e.KeyChar)) Then
Me.maskedTextBox4.Text = e.KeyChar.ToString()
Else
Me.maskedTextBox4.SelectAll()
End If
End If
End Sub

Private Sub bkpdwn_d_exit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bkpdwn_d_exit.Click
Me.DialogResult = Windows.Forms.DialogResult.OK
End Sub

--------------------------------Subroutins--------------------------------

Private Sub fill_header()
Dim cmd1 As OleDbCommand = New OleDbCommand("select bkpdwnH.bkpdwnH_date, material.material_desc, Machn.machn_desc, Shifts.shifts_desc, bkpdwnH.bkpdwnH_time, bkpdwnH.bkpdwnH_pieces_sec, bkpdwnH.bkpdwnH_Quantity, bkpdwnH.bkpdwnH_Screening from Shifts right join (Machn right join (material right join bkpdwnH on material.material_code = bkpdwnH.bkpdwnH_material_code) on Machn.machn_code = bkpdwnH.bkpdwnH_machn_code) on Shifts.shifts_code = bkpdwnH.bkpdwnH_shifts_code where bkpdwnH_id =" & w_Bkpdwn_H_ID, con)
con.Open()
Dim dr1 As OleDbDataReader = cmd1.ExecuteReader
If dr1.HasRows Then dr1.Read() Else Exit Sub
w_bkpdwnH_date.Text = dr1("bkpdwnH_date")
w_bkpdwnH_material_code.Text = dr1("material_desc")
w_bkpdwnH_machn_code.Text = dr1("machn_desc")
w_bkpdwnH_shifts_code.Text = dr1("shifts_desc")
w_bkpdwnH_time.Text = dr1("bkpdwnH_time")
w_bkpdwnH_pieces_sec.Text = dr1("bkpdwnH_pieces_sec")
w_bkpdwnH_Quantity.Text = dr1("bkpdwnH_Quantity")
w_bkpdwnH_Screening.Text = dr1("bkpdwnH_Screening")
con.Close()
End Sub

Private Sub fill_dataGrid()
Dim Bkpdwn_D_sql As String = ""
Try
Bkpdwn_D_sql = "select * from bkpdwnD where bkpdwnD_H_id =" & w_Bkpdwn_H_ID
cmd = New OleDbCommand(Bkpdwn_D_sql, con)
If con.State = ConnectionState.Open Then con.Close()
da = New OleDbDataAdapter(cmd)
ds = New DataSet()
da.Fill(ds, "Bkpdwn_D")
Bkpdwn_D_DataGridView.DataSource = ds.Tables("Bkpdwn_D")
Bkpdwn_D_DataGridView.Columns(0).HeaderText = "Κωδικός Header"
Bkpdwn_D_DataGridView.Columns(0).Visible = False
Bkpdwn_D_DataGridView.Columns(1).HeaderText = "A/A Detail"
Bkpdwn_D_DataGridView.Columns(1).Visible = False
Bkpdwn_D_DataGridView.Columns(2).HeaderText = "Κωδικός Βλάβης"
Bkpdwn_D_DataGridView.Columns(2).Width = 300
Bkpdwn_D_DataGridView.Columns(3).HeaderText = "Ημερομηνία Εναρξης Βλάβης"
Bkpdwn_D_DataGridView.Columns(3).Width = 70
Bkpdwn_D_DataGridView.Columns(4).HeaderText = "Ωρα Εναρξης Βλάβης"
Bkpdwn_D_DataGridView.Columns(4).DefaultCellStyle.Format = "hh:mm tt"
Bkpdwn_D_DataGridView.Columns(4).Width = 70
Bkpdwn_D_DataGridView.Columns(5).HeaderText = "Ημερομηνία Λήξης Βλάβης"
Bkpdwn_D_DataGridView.Columns(5).Width = 70
Bkpdwn_D_DataGridView.Columns(6).HeaderText = "Ωρα Λήξης Βλάβης"
Bkpdwn_D_DataGridView.Columns(6).DefaultCellStyle.Format = "hh:mm tt"
Bkpdwn_D_DataGridView.Columns(6).Width = 70
Bkpdwn_D_DataGridView.Columns(7).HeaderText = "Διάρκεια"
Bkpdwn_D_DataGridView.Columns(7).ReadOnly = True
Bkpdwn_D_DataGridView.Columns(7).Width = 100
Bkpdwn_D_DataGridView.Columns(8).HeaderText = "Διάρκεια" Μόνο νούμερο.
Bkpdwn_D_DataGridView.Columns(8).Visible = False
Bkpdwn_D_DataGridView.Columns(9).HeaderText = "Μηχανοδηγός"
Bkpdwn_D_DataGridView.Columns(9).Width = 160
Bkpdwn_D_DataGridView.Columns(10).HeaderText = "Συσκευαστής 1"
Bkpdwn_D_DataGridView.Columns(10).Width = 160
Bkpdwn_D_DataGridView.Columns(11).HeaderText = "Συσκευαστής 2"
Bkpdwn_D_DataGridView.Columns(11).Width = 160
Bkpdwn_D_DataGridView.Columns(12).HeaderText = "Συσκευαστής 3"
Bkpdwn_D_DataGridView.Columns(12).Width = 160
Bkpdwn_D_DataGridView.Columns(13).HeaderText = "Σχόλια"
Bkpdwn_D_DataGridView.Columns(13).Width = 250

Bkpdwn_D_DataGridView.Columns.RemoveAt(2)
Bkpdwn_D_DataGridView.Columns.Insert(2, cmb1)
With cmb1
.DataPropertyName = "bkpdwnD_code"
.HeaderText = "Κωδικός Βλάβης"
.Width = 300
End With

Bkpdwn_D_DataGridView.Columns.RemoveAt(9)
Bkpdwn_D_DataGridView.Columns.Insert(9, cmb2)
With cmb2
.DataPropertyName = "bkpdwnD_eng_driver"
.HeaderText = "Μηχανοδηγός"
.Width = 160
End With

Bkpdwn_D_DataGridView.Columns.RemoveAt(10)
Bkpdwn_D_DataGridView.Columns.Insert(10, cmb3)
With cmb3
.DataPropertyName = "bkpdwnD_packer"
.HeaderText = "Συσκευαστής 1"
.Width = 160
End With

Bkpdwn_D_DataGridView.Columns.RemoveAt(11)
Bkpdwn_D_DataGridView.Columns.Insert(11, cmb4)
With cmb4
.DataPropertyName = "bkpdwnD_packer_2"
.HeaderText = "Συσκευαστής 2"
.Width = 160
End With

Bkpdwn_D_DataGridView.Columns.RemoveAt(12)
Bkpdwn_D_DataGridView.Columns.Insert(12, cmb5)
With cmb5
.DataPropertyName = "bkpdwnD_packer_3"
.HeaderText = "Συσκευαστής 3"
.Width = 160
End With

da = Nothing
ds = Nothing
con.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
End Try
End Sub

Private Sub fill_cbos()
Dim cmd As OleDbCommand
Dim ds As New DataSet
Dim da As OleDbDataAdapter

Κωδικός Βλάβης
cmd = New OleDbCommand("select * from BreakDown", con)
If con.State = ConnectionState.Open Then con.Close()
da = New OleDbDataAdapter(cmd)
ds = New DataSet()
da.Fill(ds, "tmp_cbo1")
With cmb1
.DataSource = ds.Tables("tmp_cbo1")
.DisplayMember = "breakdown_desc"
.ValueMember = "breakdown_code"
End With

Μηχανοδηγός
cmd = New OleDbCommand("select * from employee where employee_type=1 or employee_type=9", con)
If con.State = ConnectionState.Open Then con.Close()
da = New OleDbDataAdapter(cmd)
ds = New DataSet()
da.Fill(ds, "tmp_cbo2")
With cmb2
.DataSource = ds.Tables("tmp_cbo2")
.DisplayMember = "employee_desc"
.ValueMember = "employee_id"
End With

Συσκευαστής 1
cmd = New OleDbCommand("select * from employee where employee_type=2 or employee_type=9", con)
If con.State = ConnectionState.Open Then con.Close()
da = New OleDbDataAdapter(cmd)
ds = New DataSet()
da.Fill(ds, "tmp_cbo3")
With cmb3
.DataSource = ds.Tables("tmp_cbo3")
.DisplayMember = "employee_desc"
.ValueMember = "employee_id"
End With
With cmb4
.DataSource = ds.Tables("tmp_cbo3")
.DisplayMember = "employee_desc"
.ValueMember = "employee_id"
End With
With cmb5
.DataSource = ds.Tables("tmp_cbo3")
.DisplayMember = "employee_desc"
.ValueMember = "employee_id"
End With
End Sub
End Class

View the full article
 
Back
Top