67592

DataGridComboBoxColumn shows first value on CellEnter

Question:

I am having weird results in my DataGridViewComboBoxColumn.

1) When there is no value set, and I click on the cell (not the drop down arrow) the first value in the options shows as the value, and when I click off the cell, it returns back to blank value. This is misleading as there was no value chosen, yet it shows the first value until you click off the cell.

One solution was to make the first option blank, however then I am still afflicted by the next issue...

2) If I select a cell that already has a value, then select a cell that does not, the value that was originally selected will show in the cell that has no value selected. Again, clicking off the cell will clear it, however, it is misleading again.

I would like to make this not misleading ... anyone know of a solution?

DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn(); DataTable dt = new DataTable(); dt.Columns.Add("f_Id"); dt.Columns[0].DataType = typeof(string); dt.Columns.Add("f_Desc"); dt.Columns[1].DataType = typeof(string); for (int i = 0; i < values.Count(); i++) { dt.Rows.Add(values[i], values[i]); } col.DataSource = dt; col.DisplayMember = dt.Columns[1].ColumnName; col.ValueMember = dt.Columns[0].ColumnName; col.HeaderText = header; col.Name = header; DataGridView1.Columns.Add(col);

Answer1:

This has been plaguing me for a while as well and Zeeshanef's code helped but still not not totally solve it for me. Upon further investigation, I found if you use the DefaultValuesNeeded method to set some default values, it's possible the cell value might be set but the combobox Control that gets displayed in the EditingControlShowing method has an empty value/text. I solved it by tweaking the code like this:

private void datagridview1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (e.Control is ComboBox) { ComboBox comboBox = (ComboBox)e.Control; if (datagridview1.CurrentCell.Value == null || string.IsNullOrEmpty(datagridview1.CurrentCell.Value.ToString()) || string.IsNullOrEmpty(comboBox.SelectedText) ) { comboBox.SelectedIndex = -1; } } }

Answer2:

I was also searching for this and finally solved this problem. create following event of DatagridView and set [comboboxcolumnNo] as your datagridcomboboxcolumn number.

Now whenever combobox column will get focus to edit, then it will check if current cell value is null, if it is null then it will display -1 index of combobox which shows nothing.

private void datagridview1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (datagridview1.CurrentCell.ColumnIndex == comboboxcolumnNo && e.Control is ComboBox) { ComboBox comboBox = (ComboBox)e.Control; if (datagridview1.CurrentCell.Value == null) { comboBox.SelectedIndex = -1; } } }

Recommend

  • How to join a table to two different tables [closed]
  • Getting Sqlitelog (23) not authorized error while select query in phonegap android…
  • WCF web service returning json format data
  • Primefaces Dialog Framework — dialogReturn event from menuitem
  • Slow Performance When Reading Excel With Microsoft.office.Interop.Excel
  • How can I transform an array of characters with a few lines of code to a data.frame?
  • “stack level too deep” When Processing Carrierwave Image Versions in Nested Form
  • ng-bootstrap - ngbtooltip not working with [(ngModel)]
  • Sortable Columns In scala.swing.Table
  • DataTables+RequireJS: Cannot read property 'defaults' of undefined
  • R Error - cannot change value of locked binding for 'df'
  • have mysql select statement return fully qualified column names like table.field
  • Compare a column between 2 csv files and write differences using Python
  • href inside href [duplicate]
  • use images instead of text in webgrid asp mvc
  • formediting modals shows at wrong position
  • Gforce min not supported for character in data.table
  • How to write string.Contains(someText) in expression Tree
  • Converting datatype Char to Nvarchar
  • DependencyObject.AssociatedObject is always null
  • when does setTimeout start executing in a inline
  • Adding Parent and Child Nodes in TreeView from Sql Server 2008
  • What is corresponding c++ data type to SQL numeric(18,0) data type?
  • How do I register classes by both interface and namespace with Windsor?
  • pyodbc doesn't report sql server error
  • gspread or such: help me get cell coordinates (not value)
  • Retrieving value from sql ExecuteScalar()
  • MySQL WHERE-condition in procedure ignored
  • Rearranging Cells in UITableView Bug & Saving Changes
  • align graphs with different xlab
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal