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
_______________________________________________
Mono-winforms-list maillist  -  Mono-winforms-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-winforms-list

Reply via email to