1

I want to add a custom control(Button) in current cell of Datagridview control. i have created the custom control(Button). My requirement is when i clicking any cell of the Datagridview, this control should show on that cell. here is the screen shot of this.

enter image description here

Please help me to overcome this problem. Any help appreciated.

NOTE:- This button is not a drop down button. It is just a simple button with drop down image. By clicking on this button a popup window will be open.

4

1 回答 1

2

您只需要 1 个按钮,将其设置Parent为您的DataGridView并根据当前单元格边界更新其位置。这应该在CellPainting事件处理程序中完成,如下所示:

Button button = new Button(){Width = 20, Height = 20};
int maxHeight = 20;
button.Parent = dataGridView1;//place this in your form constructor
//CellPainting event handler for both your grids
private void dataGridViews_CellPainting(object sender,
                                        DataGridViewCellPaintingEventArgs e) {
  DataGridView grid = sender as DataGridView;
  if (grid.CurrentCell.RowIndex == e.RowIndex &&
      grid.CurrentCell.ColumnIndex == e.ColumnIndex) {
     button.Top = e.CellBounds.Top - 2;
     button.Left = e.CellBounds.Right - button.Width;
     button.Height = Math.Min(e.CellBounds.Height, maxHeight);
     button.Invalidate();
  }
}
//Enter event handler for both your grids
private void dataGridViews_Enter(object sender, EventArgs e){
  button.Parent = (sender as Control);
}

注意CellPainting上面的事件处理程序(用于两个网格)应该只对button唯一做一些事情,如果您添加一些其他代码(例如绘画),两个网格都将受到该代码的影响。

于 2013-10-31T15:37:09.600 回答