0

I'm doing something to be supposedly straight and simple, yet I cannot make it work. I've an Access DB (64 bits), which is accessed by an OleDB connection. If I bind the values of a DataGridViewComboBoxColumn to a DataTable's fields ([int]id and [string]name), the ComboBoxes are rendered correctly (it displays the "DisplayMember", and keeps the "ValueMember" as an internal value). Yet, as soon as I set the target field for storing it's value with "DataPropertyName", the ComboBox now renders the "ValueMember" - or rather the "DataPropertyName" takes priority [??] - (see the attached images).

Without DataPropertyName

With DataPropertyName

The code used is shown in the following snipet:

private OleDbDataAdapter empleadosDA_local = new OleDbDataAdapter();
private OleDbDataAdapter activosDA_local = new OleDbDataAdapter();
private DBLocal dbConn_local;
internal DataSet catalogosDS, auxsDS;
internal BindingSource activosBind = new BindingSource();

......

private void BindControls() {
   DataGridViewComboBoxColumn activos_responsable1DGComboBox;
   activosBind.DataMember = "Activos";
   activosBind.DataSource = catalogosDS;

   this.Activos_dataGrid.AutoGenerateColumns = false;

   // Create DGComboBox
   activos_responsable1DGComboBox = new DataGridViewComboBoxColumn();            
   activos_responsable1DGComboBox.ValueMember = "id_empleado";
   activos_responsable1DGComboBox.DisplayMember = "nombre";
   activos_responsable1DGComboBox.DataSource = auxsDS.Tables["Responsables1"];
   activos_responsable1DGComboBox.HeaderText = "Responsable1:";
   //  activos_responsable1DGComboBox.DataPropertyName = "idEmpleado1";  <--- this is the line that creates the unexpected behaviour
   this.Activos_dataGrid.Columns.Insert(6, activos_responsable1DGComboBox);
   this.Activos_dataGrid.Columns[6].Name = "Activos_dgResponsable1";
   this.Activos_dataGrid.Columns["Activos_dgResponsable1"].Width = 195;
this.Activos_dataGrid.Columns["Activos_dgResponsable1"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;

   this.Activos_dataGrid.DataSource = activosBind;

I've done this in other Forms and it works fine using this same logic, but I just cannot find the reason why this fails. Any help will be greatly appreciated.

EDIT: JohnG asked for the schemas that feeds the DataSource of the ComboBox and the target table where it's stored, so we can identify any data type inconsistencies:

ComboBox DataSource:

enter image description here enter image description here

TargetTable which feeds the DataGridView:

enter image description here enter image description here

Javier Gómez
  • 75
  • 1
  • 7
  • Can you show the schemas for both the grids data source table AND the combo boxes data source table? Is what looks odd to me, is that when the code sets the `DisplayMember` for the combo box it uses the field `nombre`? … `activos_responsable1DGComboBox.DisplayMember = "nombre";` … So, the “name” field is called “nombre”? To me this translates to number. I am just saying that a quick glance of the code “appears” correct if the oddly named field “nombre” is actually the NAME of a person as it appears in the picture. – JohnG Jun 22 '22 at 05:31
  • hello there. Yes, I'll put both schema's. And you are righ.. "nombre" = "name": they are strings representing the name of a person. – Javier Gómez Jun 22 '22 at 13:30
  • ok... now that you made me to check the schemas, it results that the ComboBox source table has an autoincrement field (the primary key), and this is forcibly "number *long integer* and I was using for storing it a "*integer*" .. geez... thanks a lot for comming here and check my issue. I don't know if all this mess is worth to be kept here at Stackoverflow, or should I leave it for someone else having a similar issue. Thanks for the help JohnG. – Javier Gómez Jun 22 '22 at 13:46

1 Answers1

1

So it results that the ComboBox's source table has an autoincrement field (the primary key), and this is forcibly a "number long integer", and I was using for storing it a table with a field defined as integer. Setting the target field to long int solved the issue.

Javier Gómez
  • 75
  • 1
  • 7