David
David

Reputation: 1455

DataGridViewComboBoxColumn Binding

Right this is driving me nuts. I feel like I have read every post on the web with still nothing sinking in. All im trying to to is bind to a List

gvCompany.DataSource = BLL.Inventory.Company.GetAllActive(forceReload);
DataGridViewComboBoxColumn stateColumn = GridViewComboBoxColumn();
DataGridViewComboBoxCell cellCombo = new DataGridViewComboBoxCell();
stateColumn.DataPropertyName = "StateName";
stateColumn.CellTemplate = cellCombo;
gvCompany.Columns.Add(stateColumn);
 private static DataGridViewComboBoxColumn GridViewComboBoxColumn()
        {
            DataGridViewComboBoxColumn sColumn = new DataGridViewComboBoxColumn();

           sColumn.DataSource = BLL.Inventory.State.GetAllActive();
           sColumn.DisplayMember = "Name";
           sColumn.ValueMember = "OID";
            return sColumn;
        }

Can some one show me what im doing wrong? It Binds but I dont get any drop down,

Upvotes: 0

Views: 1717

Answers (3)

Guest
Guest

Reputation: 11

Make sure you enable Editing on the DataGridView. This should allow the Comobox Column to drop down.

Upvotes: 1

Lecsox
Lecsox

Reputation: 949

Your code is a little confused, this should work:

void initGridView()
{
    DataGridView gvCompany = new DataGridView();
    DataGridViewComboBoxColumn sColumn = new DataGridViewComboBoxColumn();

    //Load Data to ComboBoxColumn
    sColumn.DataSource = BLL.Inventory.State.GetAllActive();

    sColumn.DisplayMember = "Name";
    sColumn.ValueMember = "OID";
    gvCompany.Columns.Add(sColumn);

    //Load Data
    gvCompany.DataSource = BLL.Inventory.Company.GetAllActive(forceReload);
}

You should try a simple data source first to make sure there is no problem here, you can use a List<String>

You don't need to use DataGridViewComboBoxCell unless you want to specify diferent datasource to combobox in diferents lines.

Upvotes: 1

Ecyrb
Ecyrb

Reputation: 2080

Trying binding to a BindingList<T>. Sometimes weird behavior like you're seeing can be fixed by setting the DataSource to a BindingList<T> rather than a List<T>. For example, assuming your type is called Company:

gvCompany.DataSource = new BindingList<Company>(BLL.Inventory.Company.GetAllActive(forceReload));

Upvotes: 0

Related Questions