MbahAgis

Get Selected Item of DataGridViewComboBoxCell on a Single Click

Sep 7th, 2020 (edited)
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.05 KB | None | 0 0
  1. 'YouTube Video - https://www.youtube.com/watch?v=H8ryeAekLZk
  2.  
  3. Public Class Form1
  4.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  5.         Dim cmbCol As New DataGridViewComboBoxColumn
  6.         With cmbCol
  7.             .Name = "CmbCol"
  8.             .HeaderText = "Choose Item"
  9.             For i = 1 To 10
  10.                 If i Mod 2 Then
  11.                     .Items.Add("Bababa" & i)
  12.                 Else
  13.                     .Items.Add("Nanana" & i)
  14.                 End If
  15.             Next
  16.  
  17.         End With
  18.         Dim txtCol As New DataGridViewTextBoxColumn
  19.         With txtCol
  20.             .Name = "txtCol"
  21.             .HeaderText = "Output"
  22.         End With
  23.         DGV.Columns.Add(cmbCol)
  24.         DGV.Columns.Add(txtCol)
  25.     End Sub
  26.  
  27.     Private Sub DGV_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGV.CellClick
  28.         If TypeOf DGV.Columns(0) Is DataGridViewComboBoxColumn AndAlso e.ColumnIndex = 0 Then
  29.             DGV.BeginEdit(True)
  30.             DirectCast(DGV.EditingControl, ComboBox).DroppedDown = True
  31.         End If
  32.     End Sub
  33.     Private Sub DGV_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DGV.EditingControlShowing
  34.         If e.Control IsNot Nothing AndAlso DGV.CurrentCell.ColumnIndex = 0 Then
  35.             Dim cmb As ComboBox = CType(e.Control, ComboBox)
  36.  
  37.             RemoveHandler cmb.SelectedIndexChanged, AddressOf Cmb_SelectedIndexChanged
  38.             AddHandler cmb.SelectedIndexChanged, AddressOf Cmb_SelectedIndexChanged
  39.         End If
  40.     End Sub
  41.     Private Sub Cmb_SelectedIndexChanged(sender As Object, e As EventArgs)
  42.         Dim cmb As ComboBox = CType(sender, ComboBox)
  43.         If cmb.SelectedIndex > -1 Then
  44.             TxtOutput.Text = cmb.Text
  45.             DGV.Rows(DGV.CurrentCell.RowIndex).Cells(1).Value = cmb.Text
  46.         End If
  47.     End Sub
  48.     Private Sub DGV_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DGV.CurrentCellDirtyStateChanged
  49.         DGV.EndEdit()
  50.     End Sub
  51. End Class
Advertisement
Add Comment
Please, Sign In to add comment