Manoj S  Pant

Manoj S Pant

  • NA
  • 1k
  • 1.1m

Combo Box inside the DataGridView of WinForm application

Mar 13 2014 8:28 AM
Using ComboBox in DataGridView :
Implementation :
DataGridView is binding with the datatable, when use click over any row, than this row cell element convert into
combobox and now user able to select combo box elements .
Issue :
It is working fine for all the cases,
" dgvTables(e.ColumnIndex, e.RowIndex) = l_cmbTableList "
but issue is coming when e.ColumnIndex and e.RowIndex having same value, at that time combobox list item looks jumpy, user not able to select this combobox items
at first time . .
Code :
1.
Private Sub dgvTables_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvTables.CellClick
If e.ColumnIndex > -1 Then
If dgvTables.Columns(e.ColumnIndex).Name.Contains("TableCaption") Then
dgvTables(e.ColumnIndex, e.RowIndex) = l_cmbTableList ' Problem is coming at that point, dropdown list is jumpy, user not able to select item .
l_cmbTableList.DisplayMember = CONST_TableCaption
l_cmbTableList.ValueMember = CONST_TableCaption
l_cmbTableList.DataSource = dt
l_cmbTableList.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
End If
2.
Private Sub dgvTables_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvTables.EditingControlShowing
'Set the value of the object
cmbEditingComobTables = CType(e.Control, ComboBox)
If Not cmbEditingComobTables Is Nothing Then
AddHandler cmbEditingComobTables.SelectedIndexChanged, AddressOf editingCombo_SelectedIndexChanged
cmbEditingComobTables.DrawMode = DrawMode.OwnerDrawFixed
AddHandler cmbEditingComobTables.DrawItem, AddressOf cmb_DrawItem ' This attached event will fire and set the color of combo box item
End If
3.
Private Sub cmb_DrawItem(ByVal sender As System.Object, ByVal e As DrawItemEventArgs)
If Not e.Index = -1 Then
Dim cmb As ComboBox = DirectCast(sender, ComboBox)
Dim dr As DataRowView = DirectCast(cmb.Items(e.Index), DataRowView)
e.DrawBackground()
e.Graphics.DrawString(dr(cmb.DisplayMember).ToString(), e.Font, New SolidBrush(Color.Gray), e.Bounds.X, e.Bounds.Y)
End If

Answers (1)