kedar
kedar

Reputation: 103

Change to comboBox in datagridview At runtime

I am using default datagridview to display data in my windows form application.

If someone clicked on a particular column, I want to show a combo box for that cell. So at run time , user can select only fixed values for that column cell.

I want to do it on click event.

I am programmatically adding columns and rows to dataridview.

As I am adding a datable to datagridView, I am not able to add DataGridViewComboBoxColumn to datable.

Below is the code.

           using (StringReader reader = new StringReader(new  StreamReader(fileStream, Encoding.Default).ReadToEnd()))
            {
                while (reader.Peek() != -1)
                {
                    string line = reader.ReadLine();

                    if (line == null || line.Length == 0)
                        continue;

                    string[] values = line.Split(',');

                    output.Add(values);
                    if (!isColumnCreated)
                    {

                        for (int i = 0; i < values.Count() - 1; i++)
                        {

                          table.Columns.Add(values[i]);

                        }
                        isColumnCreated = true;
                        continue;
                    }

                    DataRow row = table.NewRow();
                      for (int i= 0; i < values.Count() - 1; i++)
                    {



                        row[i] = values[i];


                    }

                   table.Rows.Add(row);

                }
            }

            dataGridView1.DataSource = table;

Upvotes: 0

Views: 1629

Answers (1)

Nam B&#236;nh
Nam B&#236;nh

Reputation: 422

I do not suggest change the DataGridView cell's type on click. Just populate your DataGridView normally with your particular column as DataGridViewComboBoxColumn then try this:

(your particular column).DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;

When it is not in edit mode, the DataGridViewComboBoxCell is displayed without a drop-down button. Readmore here.

Upvotes: 2

Related Questions