Reputation: 16829
I currently have a 2-column wide DataGridView, the first column being a DataGridViewTextBoxColumn and the second a DataGridViewComboBoxColumn. I also have a pre-generated generic List (string) that is to be used as the DataSource for the DataGridViewComboBox for each row.
Finally, I have a loop that iterates through a series of strings and parses them accordingly, with extracted values being applied to respective cells using an as shown below:
dataGridView.Rows.Add("Column1Text", "Column2Text");
The gridview data is filled as expected, along with the DataGridViewComboBox properly displaying the ideal item.
The problem is, the DataGridViewComboBox, when clicked, does not drop down any items. I have checked that the DataGridViewComboBox contains items. The DataGridViewTextBoxColumn's AutoSizeMode is set to "Fill" if it's of any relevance.
Any insight as to what I may be doing wrong? Do I have to manually drop down the items when a given cell is clicked? Thanks.
Update
I have tried two different methods in terms of binding the generic list as the DataSource.
The first was binding the DataSource of the entire column itself via:
col_key.DataSource = KeyList;
The second method was binding the DataSource of each new DataGridViewComboBoxCell in the corresponding row:
(DataGridViewComboBoxCell)(row.Cells[1]).DataSource = KeyList;
Both of these methods compile and properly add the necessary items at runtime, but no items drop down when clicked.
Upvotes: 3
Views: 4511
Reputation: 528
I had a similar problem. I found setting the EditMode property of the DGV to EditOnEnter (it was previously EditProgrammatically) fixed the problem. This can be done in the designer.
Upvotes: 0
Reputation: 13
I chose to handle this in the CellEnter event:
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2)
{
DataGridViewComboBoxCell cell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewComboBoxCell;
if (cell.DataSource == null)
{
cell.DataSource = this._ComboItemsBindingSource;
cell.DisplayMember = "Value"; //lite-weight wrapper on string
cell.ValueMember = "Value"; //where Value is a property
}
}
}
Upvotes: 1
Reputation: 4348
To fill a grid combocolumn, you should treat it as regular Combo, datasource for combo should have a valuemember and a displaymember, so check the following example :
//Coded by Amen Ayach's DataClassBuilder @26/02/2012
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class iddesc{
private int _id;
public int id{
get {
return _id;
}
set {
_id = value;
}
}
private string _description;
public string description{
get {
return _description;
}
set {
_description = value;
}
}
}
To fill the grid :
private void FillData()
{
List<iddesc> DataList = new List<iddesc>();
for (int i = 1; i < 11; i++)
{
DataList.Add(new iddesc() { id = i, description = "Desc" + i.ToString() });
}
ComboCol.ValueMember = "id";
ComboCol.DisplayMember = "description";
ComboCol.DataSource = DataList;
for (int i = 0; i < 10; i++)
{
grd.Rows.Add();
grd[ComboCol.Name, i].Value = DataList[i].id;//Here you deliver the valuemember
grd[Column1.Name, i].Value = DataList[i].description;
}
}
Upvotes: 0