DataGridView ComboBox never populates
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"开发者_StackOverflow中文版 + " " + "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.
cell.DataSource = PhoneList;
cell.DisplayMember = "Number";
cell.ValueMember = "PhoneID";
Taking look at this part, what I can observe is you are setting Phone list as DataSource to cell. Now the DisplayMember
and ValueMember
properties will look for columns "Number"
and "PhoneID"
in PhoneList
. But actually PhoneList
doesn't have any columns called "Number"
and "PhoneID"
, rather PhoneList's child p1 and p2 has these columns. So it will fail to find the columns ("Number"
and "PhoneID"
) in the Datasource Phonelist.
Try populating it in a datatable and assign it to cell.
DataTable dt = new DataTable();
//Add Row p1
//Add Row p2
and now assign them.
cell.DataSource = dt;
cell.DisplayMember = "Number";
cell.ValueMember = "PhoneID";
Also see this link
Have you tried using DataGridViewComboBoxColumn like this:
DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn(); column.DataSource = PhoneList; column.DataPropertyName = "PhoneID"; column.MaxDropDownItems = 4; column.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox; yourdatagridview.Columns.Add(column);
Maybe it works in that way that combobox should contain the same list for all rows in a datagridview. At least you will know does it work when you use one PhoneList for all rows.
精彩评论