0

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 ComboBox 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
ɐsɹǝʌ ǝɔıʌ
  • 4,440
  • 3
  • 35
  • 56
Manoj S Pant
  • 85
  • 2
  • 12
  • You need to edit the data gridview template and give combo box a label which u can refer to in your code. Is your combo box refreshing page when clicked? – Redheadinferno Mar 13 '14 at 15:58
  • Thanks for your your reply, but issue is that, when I select the cell of datagridview, than I just add DataGridViewComboBoxCell object in dgvTables(e.ColumnIndex, e.RowIndex) = l_cmbTableList, Now Problem is coming when I am selecting dropdown value. not able to select item from dropdown list at first time. list get jump. – Manoj S Pant Mar 14 '14 at 03:12

0 Answers0