Javier Gómez
Javier Gómez

Reputation: 85

Unexpected behaviour with DataGridViewComboBoxColumn Value/Display members binded to DataTable from an Access DB

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

Upvotes: 0

Views: 46

Answers (1)

Javier G&#243;mez
Javier G&#243;mez

Reputation: 85

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.

Upvotes: 1

Related Questions