0

Hi guys how do you produce a gridview where first row contains text and second row contains a combobox?

i tried the code below but it renders the combobox control into a simple text saying the total item of the combobox.

        // create columns
        DataColumn column1 = new DataColumn();
        column1.Caption = "Attribute";
        column1.ColumnName = "Attribute";
        column1.DataType = typeof(string);

        DataColumn column2 = new DataColumn();
        column2.Caption = "Value";
        column2.ColumnName = "Value";
        column2.DataType = typeof(object);


        DataTable dt = new DataTable();
        dt.Columns.Add(column1);
        dt.Columns.Add(column2);

        // populate field
        DataRow row1 = dt.NewRow();
        row1.ItemArray = new object[] { "Id", "1" };

        DataRow row2 = dt.NewRow();
        row2.ItemArray = new object[] { "Name", "Vincent" };

        ComboBox cbox = new ComboBox();
        cbox.DropDownStyle = ComboBoxStyle.DropDownList;
        cbox.Items.AddRange(new object[]{1,2,3,4,5});

        DataRow row3 = dt.NewRow();
        row3.ItemArray = new object[] { "Num of Siblings",  cbox};

        DataRow row4 =dt.NewRow();
        row4.ItemArray = new object[] { "Age", "21" };

        dt.Rows.Add(row1);
        dt.Rows.Add(row2);
        dt.Rows.Add(row3);
        dt.Rows.Add(row4);

        // populate to datagridview
        dataGridView1.DataSource = dt;
        dataGridView1.Refresh();

are there any other way? i can also separate the combobox from the datagridview control like when the cell (supposed to be the combobox) is clicked, it will popup a dialogbox with combobox in it (2nd form) but this will make the user fill uncombfortable.

the user require me to have this kind of functionality and it should be in datagridview (fields got it from xml (dynamic fields)). the content of dropdown is just a sample, it will be given different values like when entering gender it will have male and female items instead.

Thanks in advance.

4

2 回答 2

0

Try this to add textbox and combobox in same column but different rows See reference

                    dataGridView1.ColumnCount = 2; // Create 2 text box columns
                    dataGridView1.Columns[0].HeaderText = "Name";
                    dataGridView1.Columns[0].Width = 350;
                    dataGridView1.Columns[1].HeaderText = "Value";
                    dataGridView1.Columns[1].Width = 150;

                    DataGridViewRow heterow0 = new DataGridViewRow();
                    DataGridViewTextBoxCell textBoxCell = new DataGridViewTextBoxCell();
                    textBoxCell.Value = "Turn Over Based On";
                    DataGridViewComboBoxCell comboCell = new DataGridViewComboBoxCell();
                    comboCell.Items.Add("Chip Transaction");
                    comboCell.Items.Add("Win/Loss");

                    heterow0.Cells.Add(textBoxCell);
                    heterow0.Cells.Add(comboCell);
                    dataGridView1.Rows.Add(heterow0); // this row has a combo in first cell
于 2013-06-22T13:53:04.210 回答
0

If you mean this...

      column1    column2   column3
 row1:combobox1,combobox2,combobox2,.....
 row2: textbox1, textbox2, textbox3,......

then this will do it

List<string> newlist = new List<string>() { "12", "34", "56", "78", "90" };

            dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Sunken;
            dataGridView1.ColumnCount = newlist.Count;

            foreach (DataGridViewColumn c in dataGridView1.Columns)
            {
                c.HeaderText = "yourvalue";
                c.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
                c.DefaultCellStyle = new DataGridViewCellStyle() { BackColor = Color.AntiqueWhite, Alignment = DataGridViewContentAlignment.MiddleCenter };

            }

            DataGridViewRow dgr = new DataGridViewRow();
            DataGridViewRow dgr1 = new DataGridViewRow();
            //DataGridViewColumnCollection dgcc = new DataGridViewColumnCollection(dataGridView1);
            foreach (string s in newlist)
            {
                DataGridViewTextBoxCell tc = new DataGridViewTextBoxCell();
                DataGridViewComboBoxCell cc = new DataGridViewComboBoxCell();
                cc.Sorted = true;
                cc.Items.Add(s);
                tc.Value = s;
                dgr.Cells.Add(cc as DataGridViewCell);
                dgr1.Cells.Add(tc);
            }

            dataGridView1.Rows.AddRange(new DataGridViewRow[] { dgr, dgr1 });

in my example i used a list but you can use what you need,just bear in mind that the foreach assigning the string to each cell was for demonstration ,you can add the values as you require.

于 2013-06-22T16:41:51.470 回答