Windows Develop Bookmark and Share   
 index > Windows Forms General > DataGridViewComboBoxCell
 

DataGridViewComboBoxCell

Hi!

I have ha promblem with adding items to a DataGridViewComboBoxCell. This is what I am doing.

private void SetUpforDataGridView()

        {

            dataGridViewSearched.ColumnCount = 5;

            dataGridViewSearched.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;

            dataGridViewSearched.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;

            dataGridViewSearched.ColumnHeadersDefaultCellStyle.Font =

                new Font(dataGridViewSearched.Font, FontStyle.Bold);



            dataGridViewSearched.Name = "Users Searched";

            dataGridViewSearched.AutoSizeRowsMode =

            DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;

            dataGridViewSearched.ColumnHeadersBorderStyle =

                DataGridViewHeaderBorderStyle.Single;

            dataGridViewSearched.CellBorderStyle = DataGridViewCellBorderStyle.Single;

            dataGridViewSearched.GridColor = Color.Black;

            dataGridViewSearched.RowHeadersVisible = false;

            dataGridViewSearched.Dock = DockStyle.Fill;



            DataGridViewComboBoxColumn dataGridViewcombobox = new DataGridViewComboBoxColumn();

            dataGridViewSearched.Columns.Add(dataGridViewcombobox);



            dataGridViewSearched.Columns[0].Name = "UserId";

            dataGridViewSearched.Columns[1].Name = "Förnamn";

            dataGridViewSearched.Columns[2].Name = "Efternamn";

            dataGridViewSearched.Columns[3].Name = "Legitimation";

            dataGridViewSearched.Columns[4].Name = "Yrkesroll";

            dataGridViewSearched.Columns[5].Name = "Enheter";

            dataGridViewSearched.Columns[5].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;



            dataGridViewSearched.Columns[0].Visible = false;

            dataGridViewSearched.Columns[0].DefaultCellStyle.Font =

            new Font(dataGridViewSearched.DefaultCellStyle.Font, FontStyle.Italic);



            dataGridViewSearched.SelectionMode =

                DataGridViewSelectionMode.FullRowSelect;

            dataGridViewSearched.MultiSelect = false;

        }

And the I am trying to set the table with an object it works fine with the text propertis but not with the DataGridViewComboBoxCell.

        public void BindUserList(List<User> userList)

        {

            dataGridViewSearched.Rows.Clear();

            object[] row = new object[5];

            

            

            foreach (User p in userList)

            {

                row[0] = p.UserId;

                row[1] = p.FirstName;

                row[2] = p.LastName;

                row[3] = p.MedicalProfession;

                row[4] = p.ProfessionalRole;

              

                dataGridViewSearched.Rows.Add(row);

                

                foreach (var unit in p.UnitActivationPeriods)

                {

                    ((DataGridViewComboBoxCell)dataGridViewSearched.Rows[dataGridViewSearched.Rows.Count - 1].Cells[5]).Items.Add(unit.ToString());

                }

                dataGridViewSearched.Rows[dataGridViewSearched.Rows.Count - 1].Tag = p;



            }

            labelNumberOfUsers.Text = userList.Count.ToString();

            dataGridViewSearched.Refresh();

        }

Thanks, Mjelten
Mjelten  Tuesday, June 23, 2009 11:02 AM
Hi! My code works fine...I just had to change the property of the datagrid to readonly = false;
Mjelten  Wednesday, June 24, 2009 12:00 PM

Hi!

I have ha promblem with adding items to a DataGridViewComboBoxCell. This is what I am doing.

private void SetUpforDataGridView()

        {

            dataGridViewSearched.ColumnCount = 5;

            dataGridViewSearched.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;

            dataGridViewSearched.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;

            dataGridViewSearched.ColumnHeadersDefaultCellStyle.Font =

                new Font(dataGridViewSearched.Font, FontStyle.Bold);



            dataGridViewSearched.Name = "Users Searched";

            dataGridViewSearched.AutoSizeRowsMode =

            DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;

            dataGridViewSearched.ColumnHeadersBorderStyle =

                DataGridViewHeaderBorderStyle.Single;

            dataGridViewSearched.CellBorderStyle = DataGridViewCellBorderStyle.Single;

            dataGridViewSearched.GridColor = Color.Black;

            dataGridViewSearched.RowHeadersVisible = false;

            dataGridViewSearched.Dock = DockStyle.Fill;



            DataGridViewComboBoxColumn dataGridViewcombobox = new DataGridViewComboBoxColumn();

            dataGridViewSearched.Columns.Add(dataGridViewcombobox);



            dataGridViewSearched.Columns[0].Name = "UserId";

            dataGridViewSearched.Columns[1].Name = "Förnamn";

            dataGridViewSearched.Columns[2].Name = "Efternamn";

            dataGridViewSearched.Columns[3].Name = "Legitimation";

            dataGridViewSearched.Columns[4].Name = "Yrkesroll";

            dataGridViewSearched.Columns[5].Name = "Enheter";

            dataGridViewSearched.Columns[5].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;



            dataGridViewSearched.Columns[0].Visible = false;

            dataGridViewSearched.Columns[0].DefaultCellStyle.Font =

            new Font(dataGridViewSearched.DefaultCellStyle.Font, FontStyle.Italic);



            dataGridViewSearched.SelectionMode =

                DataGridViewSelectionMode.FullRowSelect;

            dataGridViewSearched.MultiSelect = false;

        }

And the I am trying to set the table with an object it works fine with the text propertis but not with the DataGridViewComboBoxCell.

        public void BindUserList(List<User> userList)

        {

            dataGridViewSearched.Rows.Clear();

            object[] row = new object[5];

            

            

            foreach (User p in userList)

            {

                row[0] = p.UserId;

                row[1] = p.FirstName;

                row[2] = p.LastName;

                row[3] = p.MedicalProfession;

                row[4] = p.ProfessionalRole;

              

                dataGridViewSearched.Rows.Add(row);

                

                foreach (var unit in p.UnitActivationPeriods)

                {

                    ((DataGridViewComboBoxCell)dataGridViewSearched.Rows[dataGridViewSearched.Rows.Count - 1].Cells[5]).Items.Add(unit.ToString());

                }

                dataGridViewSearched.Rows[dataGridViewSearched.Rows.Count - 1].Tag = p;



            }

            labelNumberOfUsers.Text = userList.Count.ToString();

            dataGridViewSearched.Refresh();

        }

Thanks, Mjelten

Mjelten  Tuesday, June 23, 2009 8:31 AM
If I understand your code correctly, you are trying to add rows to the combobox for each row in the grid?

The DataGridView combobox column by default is set up to have *ONE* list of items. Each row can then have a value from that list.

So for example, you could populate the combobox column with a set of customer types. Then each row would have *one* customer type selected from that list of customer types.

Hope this helps.
www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Tuesday, June 23, 2009 4:07 PM
This is a duplicate post:

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/9fce0b7d-56be-4d8a-9e86-5134a0180f8d
www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Tuesday, June 23, 2009 4:19 PM
Ok, so if I understand you correctly I cannot have different values in each combobox?

|TEXTColumn| |TEXTColumn | |TEXTColumn |DataGridViewComboBoxColumn |
|John | |Svensson | |1231421 |Cat |
| | || | |Dog |
| | || | |mouse|
|Sarndra | |Larsson | |2344324 |Horse |
| | | | | |Bird |
|Adam | |Jönsson | |6587568 |dog |
| | || | |Horse |
| | || | |badger |
| | || | |Tiger |

This is what I want to achieve.
Mjelten  Wednesday, June 24, 2009 7:49 AM
Hi! My code works fine...I just had to change the property of the datagrid to readonly = false;
Mjelten  Wednesday, June 24, 2009 12:00 PM

You can use google to search for other answers

Custom Search

More Threads

• How to call event of running exe through another application(window)
• removing panel from tabpage and then adding it to another on tabcontrol index change.
• Extract objects inside RichTextBox
• How can I assign a string value from a variable to a dropdownlist?
• Expanding/Collapsing Panels
• WJView
• get the copycount from a printjob int Net 3.0
• ComboBox, List<> as DataSource...
• MDI Child not start maximized
• Work on Ctrl +C in PropertyGridSelectedItem