DataGridViewComboBoxColumn and databinding?
-
Hi I am using Visual Studio 2005 and MS Access 2003 to create a Windows application in C#. I have a DataGridView and I programmatically add a DataGridViewComboBoxColumn to the DataGridView. I populate the ComboBoxes in the DataGridViewComboBoxColumn from a lookup table in the database and bind the DataGridView to a DataTable loaded from another table in the database. However the correct item in the ComboBox does not get selected. In fact nothing is selected in the ComboBox. Looking at my code it feels to me that I am missing something in my code, but I do not know what. The doctorTable has a Primary key: Doctor_ID The patientTable has a Foreign key: Doctor_ID Using the following code snippets:
private void DataController()
{
try
{
//this methods loads the data from the database into two DataTables: patientTable, doctorTable
this.LoadData_Controller();this.FormatDataGridView(); dgvPatients.DataSource = patientTable; } catch (Exception ex) { throw ex; }
}
/// <summary>
/// adds a DataGridViewComboBoxColumn to the DataGridView and set the DataSource
/// </summary>
private void FormatDataGridView()
{
try
{
#region Format Doctor column//create combobox column for doctors DataGridViewComboBoxColumn comboboxColumnDoctors = new DataGridViewComboBoxColumn(); comboboxColumnDoctors.DataPropertyName = "Doctor"; comboboxColumnDoctors.HeaderText = "Doctor"; //add data to new comboboxcolumn comboboxColumnDoctors.DataSource = doctorTable; comboboxColumnDoctors.ValueMember = "Doctor\_ID"; comboboxColumnDoctors.DisplayMember = "DrName"; //insert column into datagridview dgvPatients.Columns.Insert(3, comboboxColumnDoctors); #endregion } catch (Exception ex) { throw ex; }
}
Any help will be greatly appreciated. Thanks. Kobus
-
Hi I am using Visual Studio 2005 and MS Access 2003 to create a Windows application in C#. I have a DataGridView and I programmatically add a DataGridViewComboBoxColumn to the DataGridView. I populate the ComboBoxes in the DataGridViewComboBoxColumn from a lookup table in the database and bind the DataGridView to a DataTable loaded from another table in the database. However the correct item in the ComboBox does not get selected. In fact nothing is selected in the ComboBox. Looking at my code it feels to me that I am missing something in my code, but I do not know what. The doctorTable has a Primary key: Doctor_ID The patientTable has a Foreign key: Doctor_ID Using the following code snippets:
private void DataController()
{
try
{
//this methods loads the data from the database into two DataTables: patientTable, doctorTable
this.LoadData_Controller();this.FormatDataGridView(); dgvPatients.DataSource = patientTable; } catch (Exception ex) { throw ex; }
}
/// <summary>
/// adds a DataGridViewComboBoxColumn to the DataGridView and set the DataSource
/// </summary>
private void FormatDataGridView()
{
try
{
#region Format Doctor column//create combobox column for doctors DataGridViewComboBoxColumn comboboxColumnDoctors = new DataGridViewComboBoxColumn(); comboboxColumnDoctors.DataPropertyName = "Doctor"; comboboxColumnDoctors.HeaderText = "Doctor"; //add data to new comboboxcolumn comboboxColumnDoctors.DataSource = doctorTable; comboboxColumnDoctors.ValueMember = "Doctor\_ID"; comboboxColumnDoctors.DisplayMember = "DrName"; //insert column into datagridview dgvPatients.Columns.Insert(3, comboboxColumnDoctors); #endregion } catch (Exception ex) { throw ex; }
}
Any help will be greatly appreciated. Thanks. Kobus
Check DataBindings property. I know combobox has this property and hopefully DataGridViewComboBox has it as well. Here you can make each control be ware of the other. If property is not there then simply write some code. For example, when the comboBox is selected, select the appropriate patients which have the foreign key.
-
Hi I am using Visual Studio 2005 and MS Access 2003 to create a Windows application in C#. I have a DataGridView and I programmatically add a DataGridViewComboBoxColumn to the DataGridView. I populate the ComboBoxes in the DataGridViewComboBoxColumn from a lookup table in the database and bind the DataGridView to a DataTable loaded from another table in the database. However the correct item in the ComboBox does not get selected. In fact nothing is selected in the ComboBox. Looking at my code it feels to me that I am missing something in my code, but I do not know what. The doctorTable has a Primary key: Doctor_ID The patientTable has a Foreign key: Doctor_ID Using the following code snippets:
private void DataController()
{
try
{
//this methods loads the data from the database into two DataTables: patientTable, doctorTable
this.LoadData_Controller();this.FormatDataGridView(); dgvPatients.DataSource = patientTable; } catch (Exception ex) { throw ex; }
}
/// <summary>
/// adds a DataGridViewComboBoxColumn to the DataGridView and set the DataSource
/// </summary>
private void FormatDataGridView()
{
try
{
#region Format Doctor column//create combobox column for doctors DataGridViewComboBoxColumn comboboxColumnDoctors = new DataGridViewComboBoxColumn(); comboboxColumnDoctors.DataPropertyName = "Doctor"; comboboxColumnDoctors.HeaderText = "Doctor"; //add data to new comboboxcolumn comboboxColumnDoctors.DataSource = doctorTable; comboboxColumnDoctors.ValueMember = "Doctor\_ID"; comboboxColumnDoctors.DisplayMember = "DrName"; //insert column into datagridview dgvPatients.Columns.Insert(3, comboboxColumnDoctors); #endregion } catch (Exception ex) { throw ex; }
}
Any help will be greatly appreciated. Thanks. Kobus