2

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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Mike Malter
  • 1,018
  • 1
  • 14
  • 38
  • Try using a **BindingSource** for the Grid like cell.DataSource = new BindingSource(PhoneList, null); – V4Vendetta Apr 18 '11 at 05:38
  • Thank you for your answer. I tried it, but the ComboBox is still not populated. Do you have any other thoughts? – Mike Malter Apr 18 '11 at 05:51
  • In which event are you populating the grid ? – V4Vendetta Apr 18 '11 at 13:40
  • treeViewMain_AfterSelect. I click on a tree view node and in that event, I have my grid populate code. Do you think it should be somewhere else? – Mike Malter Apr 18 '11 at 17:12
  • Does anybody know how to do this? – Mike Malter Apr 18 '11 at 21:04
  • @Mike: What you tried works for me, Are you sure the **PhoneList** is not **empty** i think its something trivial – V4Vendetta Apr 19 '11 at 05:32
  • V4Vendetta, yes I have checked PhoneList in the debugger thinking the same as you. Everything I have read says this should work, but it just never does. I tried using data tables as well as arrays. Would it be possible that I could see a snippet of working code from you? I am perplexed, I can get everything to work but filling the ComboBox. Thank you. – Mike Malter Apr 19 '11 at 14:32
  • @Mike : void treeView1_AfterSelect(object sender, TreeViewEventArgs e){ int rowindx = dataGridView1.Rows.Add();List lst = new List();lst.Add(new Employee(10, "This", "Framework"));lst.Add(new Employee(15, "Is a", "Version2"));lst.Add(new Employee(20, "Demo", "Only")); (dataGridView1[0, rowindx] as DataGridViewComboBoxCell).DataSource = lst;(dataGridView1[0, rowindx] as DataGridViewComboBoxCell).DisplayMember = "ID";GridView1[0, rowindx] as DataGridViewComboBoxCell).ValueMember = "Name";(dataGridView1[3, rowindx] as DataGridViewTextBoxCell).Value= "I can see this too";} – V4Vendetta Apr 20 '11 at 04:40
  • V4, thanks for the sample. I'll try it tomorrow and let you know. Thank you again. – Mike Malter Apr 20 '11 at 05:43

2 Answers2

0
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

Community
  • 1
  • 1
Marshal
  • 6,551
  • 13
  • 55
  • 91
  • Did you try with the list, i think you are mistaken here – V4Vendetta Apr 18 '11 at 07:03
  • 1
    Your link is for Datatable what i meant is for a List of your class Which is possible.If you want try this public class Employee { private int _id; public int ID { get { return _id; } set { _id = value; } } private string _name; public string Name { get { return _name; } set { _name = value; } } private string _class; public string Class { get { return _class; } set { _class = value; } } } – V4Vendetta Apr 18 '11 at 07:31
  • @V4Vendetta: I think you are right with class. I just referred some more articles. I remove the explanation part from answer – Marshal Apr 18 '11 at 07:39
  • Niraj, creating a DataTable and then adding rows did not work. I can see the rows in the DataTable in the debugger, but I cannot see any data in the ComboBox column. Any other thoughts? – Mike Malter Apr 18 '11 at 19:02
0

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.

Blablablaster
  • 3,238
  • 3
  • 31
  • 33