DataGridView绑定数据后如何再将某列设成DataGridViewComboBoxColumn
数据已经绑定到DataGridView了,但是要把某列设成combobox可选择的样式,要怎样实现呢?
------解决方案-------------------- private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].HeaderText == "审核 ")
{
//DataGridViewComboBoxCell cl = new DataGridViewComboBoxCell();
//try { cl = (DataGridViewComboBoxCell)(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]); }
//catch
//{
DataGridViewComboBoxCell cel = new DataGridViewComboBoxCell();
//cel.Style.
cel.Style.BackColor = Color.Cornsilk;
cel.Items.Add( "已审核 ");
cel.Items.Add( "未审核 ");
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] = cel;
//cel.ValueMember = "已审核 ";
//}
}
}
------解决方案--------------------示例:在DataGridView的CurrentCellChanged事件中:
try
{
Rectangle ree = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, false);
int cols = this.dataGridView1.CurrentCell.ColumnIndex;
this.comboCplb.Visible = false;
this.comboCanDi.Visible = false;
switch (cols)
{
case 4://产品类别
{
this.comboCplb.Left = ree.Left;
this.comboCplb.Top = this.dataGridView1.Top + ree.Top;
this.comboCplb.Width = ree.Width;
this.comboCplb.Height = ree.Height;
this.comboCplb.DropDownWidth += 20;
this.comboCplb.Visible = true;
this.comboCplb.Focus();
break;
}
default:
{
// this.comboDw.Visible = false;
this.comboCplb.Visible = false;
this.comboCanDi.Visible = false;
break;
}
}
}
catch
{ }