Reputation: 19
Can someone explain to me how to add DataGridViewComboBoxCell to dataGridView? Code is something like this:
foreach(....){
DataGridViewComboBoxCell cmb = new DataGridViewComboBoxCell();
//cmb.item.add(....
dataGridView.Rows.Add(new object[] {cmb,name,surname}); }
First cell in grid is type of DataGridViewComboBoxColumn, I tried with changing cmb to DataGridViewComboBoxColumn and still nothing.
I handeld DataError so i dont get "value is not valid" error, but my comboboxes in datagridview are empty.
Upvotes: 0
Views: 10491
Reputation: 1
I encountered kind of similar issue,
DataGridViewComboBoxCell
value is not valid.
The combobox works fine in the first run, and when I change the value in one of the comboboxes (which is outside of the grid), changing that, should bind the new set of data to the datagrid based on the parameter. But it throws the error at second run.
This is what worked for me:
foreach (var p in realmPos)
{
clmRealmPosition.Items.Add(p.RealmPosition);
}
I replaced above code with:
clmRealmPosition.DataSource = realmPos.Select(s => s.RealmPosition).ToList();
Note: clmRealmPosition
is the Design name for the combobox column. And also it's good to have an ErrorEvent
as well.
Upvotes: 0
Reputation: 26
There are different approaches on how to solve this problem. I post this answer because I was dealing with a similar situation and found a decent solution. A DataGridView must be placed on your formular, the rest (in this example) is done in code. First of all we start with our DataSource for the DataGridView.
DataTable myData = new DataTable();
myDataGridView.AutoGenerateColumns = false;
It´s important to turn off the auto generation, so we can set up the columns as we want. Now add some data to our DataTable . In this example cats:
myData.Columns.Add("Name", typeof(string));
myData.Columns.Add("Gender", typeof(string));
myData.Rows.Add(new object[] {"Ser Pounce", "male"});
myData.Rows.Add(new object[] {"Fluffy", "male"});
myData.Rows.Add(new object[] {"Peach", "female"});
After we have specified the Data and its columns we can create the specific columns in our DataGridView.
DataGridViewTextBoxColumn nameColumn = new DataGridViewTextBoxColumn();
nameColumn.HeaderText = "Cat name";
nameColumn.DataPropertyName = "Name";
nameColumn.ReadOnly = true;
DataGridViewComboBoxColumn genderColumn = new DataGridViewComboBoxColumn ();
List<string> genderList = new List<string>() { "male", "female", "unknown" };
genderColumn.DataSource = genderList;
genderColumn.HeaderText = "Gender";
genderColumn.DataPropertyName = "Gender";
genderColumn.ValueType = typeof(string);
myDataGridView.DataSource = myData;
myDataGridView.Columns.AddRange(nameColumn, genderColumn);
Add our Data as DataSource for the DataGridView before adding the defined columns. Note that the DataPropertyName of each column (DataGridView) has to be the same as the name we gave the columns in our DataTable. HeaderText can be named differently. Lastly match the ValueType of the column and data.
Upvotes: 1
Reputation: 19
Ok, I solved the problem. It seems that you have to add values to cells step by step.
I'm going to give general explanation because it seems that a loot of people have problem with this. Let's say you have DataGridView with 3 columns, DataGridViewTextBoxCell, DataGridViewComboBoxColumn, DataGridViewCheckBoxCell in that order. Now, YOU HAVE to make make this three columns using Desinger or otherwise it wont work.
So now you want to add specific values to grid, each row representing lets say a person. In designer it looks like
Name PhoneNumberS Married
..*.. |.....|..............|.........|....
So you want to add his name to textboxcell, list of phonenumbers to comboboxcell and to check checkboxcell if his married. And to repeat for each person you have in your list.
Here is pseudocode:
foreach(Person p in people.getAll()){
/////MAKE NEW CELL FOR EACH VALUE
DataGridViewTextBoxCell name= new DataGridViewTextBoxCell();
name.Value = p.name;
DataGridViewTextBoxCell phones= new DataGridViewTextBoxCell();
foreach(Int Pnumber in p.numbers){
phones.items.add(Pnumber);
}
DataGridViewCheckBoxCell ismarried = new DataGridViewCheckBoxCell();
ismarried.Value = p.married;
///////// MAKE NEW ROW AND ADD CELLS
DataGridViewRow row = new DataGridViewRow();
row .Cells.Add(name);
row .Cells.Add(phones);
row .Cells.Add(ismarried );
///// ADD ENTIRE ROW TO DATA GRID
dataGridView.Rows.Add(row);
}
Just to repeat, you FIRST HAVE TO ADD COLUMNS TO GRID USING DESIGNER and when you add cells to row in code it HAS TO BE IN EXACLY SAME ORDER AS SEEN IN DESIGNER.
Upvotes: 1
Reputation: 256
I think that you want to add a column of type DataGridViewComboBoxColumn.
DataGridViewComboBoxColumn d = new DataGridViewComboBoxColumn();
d.Name = "Drinks";
d.Items.Add("Coca-Cola");
d.Items.Add("Sprite");
dataGridView1.Columns.Add(d);
I hope it will help.
Upvotes: 0