Winforms DataGridView自定义控件启用按钮处于未编辑模式

Winforms DataGridView自定义控件启用按钮处于未编辑模式,winforms,datagridview,Winforms,Datagridview,有人知道如何使按钮始终可见吗?(不仅仅是在单元格的编辑模式下) 我想请你注意这个问题的答案。 我可以增强此解决方案,以便始终看到单元格中的按钮控件。我想得到的是第一次点击单元格的弹出框。这是在未编辑模式下绘制按钮的代码 // Finally paint the NumericUpDown control Rectangle srcRect = new Rectangle(0, 0, valBounds.Width, valBounds.Height);

有人知道如何使按钮始终可见吗?(不仅仅是在单元格的编辑模式下) 我想请你注意这个问题的答案。

我可以增强此解决方案,以便始终看到单元格中的按钮控件。我想得到的是第一次点击单元格的弹出框。这是在未编辑模式下绘制按钮的代码

 // Finally paint the NumericUpDown control
                    Rectangle srcRect = new Rectangle(0, 0, valBounds.Width, valBounds.Height);
                    if (srcRect.Width > 0 && srcRect.Height > 0)
                    {
                        Bitmap renderingBitmap = new Bitmap(22, 18);
                        new TextButton().button.DrawToBitmap(renderingBitmap, srcRect);
                        graphics.DrawImage(renderingBitmap, new Rectangle(new Point(cellBounds.X+cellBounds.Width-24, valBounds.Location.Y-2), valBounds.Size),
                                           srcRect, GraphicsUnit.Pixel);
                    }

更好的选择是在DataGridView上嵌入一个按钮。这将使您能够更好地控制DataGridView的使用。请参见以下代码段:

Button b1 = new Button();
int cRow = 0, cCol = 0;
private void Form1_Load(object sender, EventArgs e)
{
  b1.Text = "...";
  b1.Visible = false;
  this.dataGridView1.Controls.Add(b1);
  b1.BringToFront();
  this.dataGridView1.Paint += new PaintEventHandler(dataGridView1_Paint);
  this.b1.Click += new EventHandler(b1_Click);
}
void b1_Click(object sender, EventArgs e)
{
  //Implement your logic here
}
void dataGridView1_Paint(object sender, PaintEventArgs e)
{
  if(cRow != 0 && cCol != 0)
  {
    Rectangle rect = new Rectangle();
    rect = dataGridView1.GetCellDisplayRectangle(cRow ,cCol , false);
    rect.X = rect.X + (2*dataGridView1.Columns[1].Width / 3);
    rect.Width = dataGridView1.Columns[1].Width / 3;
    b1.Bounds = rect;
    b1.Visible = true;
  }
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
  cRow = e.RowIndex;
  cCol = e.ColumnIndex;
}
在上面的代码片段中,省略号按钮的位置设置为上次单击的单元格。单击单元格后,可见性始终为真。在我看来,这将提供一个更好的控制按钮的功能,更容易维护