public static void DataGridAddCobx(DataGridView DataGD, ComboBox Cobx)
{
try
{
DataGD.Controls.Add(Cobx);
Rectangle rect = DataGD.GetCellDisplayRectangle(DataGD.CurrentCell.ColumnIndex, DataGD.CurrentCell.RowIndex, false);
Cobx.Left = rect.Left;
Cobx.Top = rect.Top;
Cobx.Width = rect.Width;
Cobx.Height = rect.Height;
Cobx.Visible = true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
调用:
如第三列动态加入comboBox1
if (this.dataGridView1.CurrentCell.ColumnIndex == 2)
{
DataGridAddComBox(this.dataGridView1, comboBox1);
}
else
{
this.comboBox1.Visible = false;
}