[Mono-winforms-list] DataGridViewComboboxColumn does not respect DisplayMember

W. de Hoog wdehoog at exalondelft.nl
Fri Sep 13 19:54:57 UTC 2013


Hi,

A long time bug is about DataGridViewComboboxColumn not respecting 
DisplayMember when using a DataSource.

Since I am trying to get my DataGridView working under mono I tried to 
get it working and it sort of works when deriving 
DataGridViewComboBoxCell and overriding GetFormattedValue and 
ParseFormattedValue.

There the DataSource can be used as a lookup table to get the value to 
be displayed or stored.

It is not a real solution but this approach could be useful for others 
as well.

In order for the DataGridViewComboBoxCell to have all the required 
properties it needs to be set on the DataGridViewComboboxColumn asap so 
it get's all the properties from it:
       DataGridViewComboBoxColumn comboColumn = new 
DataGridViewComboBoxColumn();
       comboColumn.CellTemplate = new MyDataGridViewComboBoxCell();
       comboColumn.Name = "EmployeeId";
       comboColumn.DataPropertyName = "EmployeeId";
       comboColumn.ValueType = typeof(int);
       comboColumn.ValueMember = "EmployeeId";
       comboColumn.DisplayMember = "Name";
       comboColumn.DataSource = createEmployeeTable();
       dgv.Columns.Add(comboColumn);


GetFormattedValue looks like:
         object obj = null;
         DataTable dtable = this.DataSource as DataTable;
         if(dtable != null) {
           foreach(DataRow row in dtable.Rows) {
             if(row[this.ValueMember].Equals(value)) {
               obj = row[this.DisplayMember];
               break;
             }
           }
         }
         if(obj == null)
           obj = base.GetFormattedValue(value, rowIndex, ref cellStyle, 
valueTypeConverter, formattedValueTypeConverter, context);
         return obj;


and ParseFormattedValue:
         object obj = null;
         DataTable dtable = this.DataSource as DataTable;
         if(dtable != null) {
           // look for display value
           foreach(DataRow row in dtable.Rows) {
             if(row[this.DisplayMember].Equals(formattedValue)) {
               obj = row[this.ValueMember];
               break;
             }
           }
         }
         if(obj == null)
           obj = base.ParseFormattedValue(formattedValue, cellStyle, 
formattedValueTypeConverter, valueTypeConverter);
         return obj;


regards,

-- 
Willem-Jan de Hoog


More information about the Mono-winforms-list mailing list