当我运行我的代码时,dataGridView TopLeftHeaderCell也有一个组合框.我怎么能改变呢?
这是我的代码:
public void AddHeaders(DataGridView dataGridView)
{
for (int i = 0; i < 4; i++)
{
// Create a ComboBox which will be host a column's cell
ComboBox comboBoxHeaderCell = new ComboBox();
comboBoxHeaderCell.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxHeaderCell.Visible = true;
foreach (KeyValuePair<string, string> label in _labels)
{
comboBoxHeaderCell.Items.Add(label.Key);
}
// Add the ComboBox to the header cell of the column
dataGridView.Controls.Add(comboBoxHeaderCell);
comboBoxHeaderCell.Location = dataGridView.GetCellDisplayRectangle(i, -1, true).Location;
comboBoxHeaderCell.Size = dataGridView.Columns[0].HeaderCell.Size;
comboBoxHeaderCell.Text = _labels[i].Key;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
在你的代码中,
comboBoxHeaderCell.Location = dataGridView.GetCellDisplayRectangle(i, -1, true).Location;
Run Code Online (Sandbox Code Playgroud)
总是会返回0,0,因此您将您的ComboBoxat 位置0,0放入 中DataGridView,这就是我们看到这个的原因

您可以dataGridView1[i,0].size根据需要的尺寸使用
我正在寻找地点
我找不到,但你可以做的是使用dataGridView1.Width - dataGridView1[1,0].Size.Width
你可以使用宽度,并删除所有标题宽度的大小,然后一一添加它们。
int xPos = dataGridView1.Width;
for (int i = 0; i < 4; i++)
{
xPos -= dataGridView1[i, 0].Size.Width;
}
...
comboBoxHeaderCell.Size = dataGridView.Columns[0].HeaderCell.Size;
comboBoxHeaderCell.Location = new Point(xPos, 0);
xPos += comboBoxHeaderCell.Size.Width;
Run Code Online (Sandbox Code Playgroud)