Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView combobox column bound to list
 

DataGridView combobox column bound to list

I've searched extensively for an answer in the forum, but everyone is using a database to do this, I want to use a list, IList, IBindingList, or some such.

I have a DataGridView and I want one of the columns to be a combobox. So I set the column type to be DataGridViewComboBoxColumn, and I've tried many different combinations but I can't get the combo box to populate.

For example, my latest attempt: In the Bound Column Properties, I click on "DataSource", Add Project Datasource, and I select Object, and then select a class named "Products" which derives from BindingList<Product>. The constructor of Products populates itself with several Product objects.

The designer seems to understand what I'm getting at, because the DisplayMember and ValueMember pulldowns show the public properties of Product. I select a property named Type, a string, for both DisplayMember and DisplayValue.

When I run the program, the combobox in that column is not populated.

What am I doing wrong?

The DataGridView is successfully bound to it's source, and when I set the column in question to be a textbox, it properly maps to the member I have selected in DataPropertyName.
iiwoeirie  Saturday, September 05, 2009 1:57 AM

Hi,

If only bind the datasource to the class, there is no data in the class and also set the DataPropertyName, it will throw an error. This is because the combobox can’t find the corresponding displayvalue in the datasource.

I wrote the following example for you.

I create two bindinglists as the DataGridView datasource and ComboboxColumn datasource. You need to make sure the corresponding datasouce of comboboxcolumn.valuemember contains the bound datavalue of comboboxcolumn. Otherwise it will throw an error.

private void Form1_Load(object sender, EventArgs e)

{

BindingList<Customer> customList = new BindingList<Customer>();

customList.Add(new Customer(1, 1, "aa"));

customList.Add(new Customer(2, 1, "bb"));

customList.Add(new Customer(3, 2, "cc"));

customList.Add(new Customer(4, 1, "dd"));

BindingList<GenderDetial> genderList = new BindingList<GenderDetial>();

genderList.Add(new GenderDetial(1, "male"));

genderList.Add(new GenderDetial(2, "female"));

dataGridView1.AutoGenerateColumns = false;

dataGridView1.DataSource = customList;

DataGridViewTextBoxColumn txtID = new DataGridViewTextBoxColumn();

txtID.DataPropertyName = "Id";

txtID.Name = "ID";

txtID.HeaderText = "Custom ID";

DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();

cmb.DataPropertyName = "GenderID";

cmb.Name = "Gender";

cmb.HeaderText = "Gender";

cmb.DataSource = genderList;

cmb.ValueMember = "GenderID";

cmb.DisplayMember = "Gender";

DataGridViewTextBoxColumn txtName = new DataGridViewTextBoxColumn();

txtName.DataPropertyName = "Name";

txtName.Name = "Name";

txtName.HeaderText = "Custom Name";

dataGridView1.Columns.AddRange(txtID, cmb, txtName);

}

}

public class Customer

{

private int _id;

private int _genderID;

private string _name;

public Customer(int customID, int customGenderID, string customName)

{

this.Id = customID;

this.GenderID = customGenderID;

this.Name = customName;

}

public int Id

{

get { return _id; }

set { _id = value; }

}

public int GenderID

{

get { return _genderID; }

set { _genderID = value; }

}

public string Name

{

get { return _name; }

set { _name = value; }

}

}

public class GenderDetial

{

private int _genderID;

private string _gender;

public GenderDetial(int id, string genderDetial)

{

this.GenderID = id;

this.Gender = genderDetial;

}

public int GenderID

{

get { return _genderID; }

set { _genderID = value; }

}

public string Gender

{

get { return _gender; }

set { _gender = value; }

}

}

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Friday, September 11, 2009 2:38 PM
Just in case... might be worth setting it up so the objects are created before form creation, or as the first thing in the form constructor... it might be that at form construction time it doesn't see any objects so doesn't populate the combo box. I have run into stuff that works like that before, though not specifically with combo boxes.

Randell
RandellP  Tuesday, September 08, 2009 7:58 PM

Hi,

If only bind the datasource to the class, there is no data in the class and also set the DataPropertyName, it will throw an error. This is because the combobox can’t find the corresponding displayvalue in the datasource.

I wrote the following example for you.

I create two bindinglists as the DataGridView datasource and ComboboxColumn datasource. You need to make sure the corresponding datasouce of comboboxcolumn.valuemember contains the bound datavalue of comboboxcolumn. Otherwise it will throw an error.

private void Form1_Load(object sender, EventArgs e)

{

BindingList<Customer> customList = new BindingList<Customer>();

customList.Add(new Customer(1, 1, "aa"));

customList.Add(new Customer(2, 1, "bb"));

customList.Add(new Customer(3, 2, "cc"));

customList.Add(new Customer(4, 1, "dd"));

BindingList<GenderDetial> genderList = new BindingList<GenderDetial>();

genderList.Add(new GenderDetial(1, "male"));

genderList.Add(new GenderDetial(2, "female"));

dataGridView1.AutoGenerateColumns = false;

dataGridView1.DataSource = customList;

DataGridViewTextBoxColumn txtID = new DataGridViewTextBoxColumn();

txtID.DataPropertyName = "Id";

txtID.Name = "ID";

txtID.HeaderText = "Custom ID";

DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();

cmb.DataPropertyName = "GenderID";

cmb.Name = "Gender";

cmb.HeaderText = "Gender";

cmb.DataSource = genderList;

cmb.ValueMember = "GenderID";

cmb.DisplayMember = "Gender";

DataGridViewTextBoxColumn txtName = new DataGridViewTextBoxColumn();

txtName.DataPropertyName = "Name";

txtName.Name = "Name";

txtName.HeaderText = "Custom Name";

dataGridView1.Columns.AddRange(txtID, cmb, txtName);

}

}

public class Customer

{

private int _id;

private int _genderID;

private string _name;

public Customer(int customID, int customGenderID, string customName)

{

this.Id = customID;

this.GenderID = customGenderID;

this.Name = customName;

}

public int Id

{

get { return _id; }

set { _id = value; }

}

public int GenderID

{

get { return _genderID; }

set { _genderID = value; }

}

public string Name

{

get { return _name; }

set { _name = value; }

}

}

public class GenderDetial

{

private int _genderID;

private string _gender;

public GenderDetial(int id, string genderDetial)

{

this.GenderID = id;

this.Gender = genderDetial;

}

public int GenderID

{

get { return _genderID; }

set { _genderID = value; }

}

public string Gender

{

get { return _gender; }

set { _gender = value; }

}

}

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Friday, September 11, 2009 2:38 PM

You can use google to search for other answers

Custom Search

More Threads

• Datagrid Best Practice?
• How to handle pending updates
• RowState changed by just traverse table in DataGridView
• Access sql mobile from winforms applicacion?
• Adding New Records
• Can I reset, or replace the ReadOnly ColumnIndex property of the Datagridview.?
• ASP.Net Related Questions
• Invalid NewIndex in ListChange Event when ListChangedType is ItemDeleted
• Drag-and-drop using DataGridView.....
• Scrollbar on DataGridView stops working after data refresh