在列标题中添加comboBox

时间:2016-12-14 11:58:00

标签: vb.net windows

您好我正在使用vb创建我的应用程序,我想将组合框添加到列标题,如下图所示

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为这是一个重复的问题。见How to Add Combobox in datagridview only for header?

但是这个链接还有一些很好的示例代码,我在下面复制了这些代码。 MSDN: Dropdown/ComboBox Column Header Cell

    public partial class Form1 : Form
    {
        private void Form1_Load(object sender, EventArgs e)
        {
                // Create a ComboBox which will be host in column1's cell
                ComboBox comboBoxHeaderCell1 = new ComboBox();
                comboBoxHeaderCell1.DropDownStyle = ComboBoxStyle.DropDownList;
                comboBoxHeaderCell1.Visible = true;
                comboBoxHeaderCell1.Items.Add("Column1");
                comboBoxHeaderCell1.Items.Add("Column2");

                // Add the ComboBox to the header cell of column1
                dataGridView1.Controls.Add(comboBoxHeaderCell1);            
                comboBoxHeaderCell1.Location = this.dataGridView1.GetCellDisplayRectangle(0, -1, true).Location;
                comboBoxHeaderCell1.Size = this.dataGridView1.Columns[0].HeaderCell.Size;
                comboBoxHeaderCell1.Text = "Column1";
        }
    }