2016-12-14 30 views

回答

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"; 
     } 
    } 
相关问题