I have a column in a DataGridView that is a ComboBox, and I cannot get it to populate. I have looked at my code over and over again, and it seems right, but can't be because the ComboBox is never populated.
Here is my code.
First, a static DataSource for testing:
List<Phone> PhoneList = new List<Phone>();
Phone p1 = new Phone();
p1.PhoneID = 1;
p1.PhoneTypeID = 2;
p1.AreaCode = "333";
p1.Number = "123-1234";
p1.PhoneTypeName = "Primary";
PhoneList.Add( p1 );
Phone p2 = new Phone();
p2.PhoneID = 2;
p2.PhoneTypeID = 2;
p2.AreaCode = "444";
p2.Number = "432-8900";
p2.PhoneTypeName = "Secondary";
PhoneList.Add( p2 );
This just to show the DataPropertyName of the ComboBox column:
this.dgvClinic.Columns[ "PhoneName" ].DataPropertyName = "PhoneID";
Next, I'm pulling data and then for each row of it, I am adding a row to the DataGridView and populating the cell for the newly added row. At the end is my ComboBox column, and as you can see, I'm populating my DataSource, DisplayMember and ValueMember properties. Please notice the three commented out rows at the end as I even tried adding static values to the cell's Item collection. Neither approach worked. All that happens is that the first three columns have data, but the ComboBox cell never has any data.
Clinic c = new Clinic();
string CurrentLocation = string.Empty;
foreach ( Clinic i in c.SelfListAll() )
{
Location_List_ByClinicResult l = i.Locations.FirstOrDefault<Location_List_ByClinicResult>();
if ( l == null )
{
CurrentLocation = "12345 Main Street" + " " + "San Rafael" + ", " + "CA" + " " + "94903";
}
else
{
CurrentLocation = l.Address1 + " " + l.City + ", " + l.StateAbbrev + " " + l.Zip;
}
RowIndex = this.dgvClinic.Rows.Add();
this.dgvClinic.Rows[ RowIndex ].Cells[ "ClinicID" ].Value = i.ClinicID.ToString();
this.dgvClinic.Rows[ RowIndex ].Cells[ "ClinicName" ].Value = i.Name;
this.dgvClinic.Rows[ RowIndex ].Cells[ "LocationName" ].Value = CurrentLocation;
DataGridViewComboBoxCell cell = this.dgvClinic.Rows[ RowIndex ].Cells[ "PhoneName" ] as DataGridViewComboBoxCell;
cell.DataSource = PhoneList;
cell.DisplayMember = "Number";
cell.ValueMember = "PhoneID";
//cell.Items.Add( "One" );
//cell.Items.Add( "Two" );
//cell.Items.Add( "Three" );
}
I'm really hoping that someone can see what I am missing here. By the way, I have created the columns in the designer.
Thanks.