通过设置Combox的坐标位置实现在datagridview中单击单元格显示下拉框

在datagridview的列设置里,可以设置成下拉类型的,但是有时这个往往不能满足我们的要求,比如需要一些自定义的下拉框,这时想到利用单击单元格来设置下拉框的位置和设置visible来实现。首先获取datagridview的坐标位置,然后获取单元格的Rectangle的相对位置,然后将两个位置相加,就得到单元格的实际屏幕坐标。另外,结构体的LIST绑定到datagridview,是不能改变数据的,可以用类。

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2&&e.RowIndex>-1)
{
int dgvx=dataGridView1.Location.X;
int dgvy=dataGridView1.Location.Y;
int cellx=dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,e.RowIndex,false).X;
int celly=dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,e.RowIndex,false).Y;
categoryDropDownList1.Location = new Point(dgvx + cellx, dgvy + celly);
categoryDropDownList1.Visible = true;
var source = bindingSource1.DataSource as List<ProductCategoryInfor>;
if (!string.IsNullOrEmpty(source[e.RowIndex].CategoryID))
{
categoryDropDownList1.SelectedValue = source[e.RowIndex].CategoryID;
}
else categoryDropDownList1.SelectedIndex = 0;
}
else categoryDropDownList1.Visible = false;
}

 

posted on 2012-02-18 00:20  三才者  阅读(821)  评论(0)    收藏  举报