lundi 11 mai 2015

Drop down values are not getting listed in combobox in winform

I am using data grid control in windows application. I want to add one column as combo box which will load the drop down values from a data table. below code I am using to create it.

                        DataTable Dt = new DataTable("Table1");
                        Dt.Columns.Add("Id");
                        Dt.Columns.Add("Value");
                        Dt.Rows.Add(new object[] { "1", "Item1" });
                        Dt.Rows.Add(new object[] { "2", "Item2" });
                        dataGridView1.AllowUserToAddRows = false;
                        dataGridView1.ColumnCount = 4;
                        dataGridView1.Columns[0].Name = "col1";
                        dataGridView1.Columns[1].Name = "col2";
                        dataGridView1.Columns[2].Name = "col3";
                        DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
                        cmb.HeaderText = "Item";
                        cmb.Name = "cmb";
                        cmb.MaxDropDownItems = 4;

                        cmb.DataSource = Dt;
                        cmb.DisplayMember = "Value";
                        dataGridView1.Columns.Add(cmb);



                        while (reader.Read())
                        {
                            int number = dataGridView1.Rows.Add();
                            dataGridView1.Rows[number].Cells[0].Value = reader[0].ToString();
                            dataGridView1.Rows[number].Cells[1].Value = reader[1].ToString();
                            dataGridView1.Rows[number].Cells[2].Value = reader[2].ToString();
                            DataGridViewComboBoxCell ComboColumn = (DataGridViewComboBoxCell)(dataGridView1.Rows[number].Cells[4]);
                            //ComboColumn.DataSource = Dt;
                            //ComboColumn.DisplayMember = "Value";
                            ComboColumn.Selected = true;
                        }

When i try to run this It gives me combo box with drop down but no values of item are loaded in it. Can somebody help?

Aucun commentaire:

Enregistrer un commentaire