0

Does anyone know how to get the button visible all the time?.(Not only in edit mode of the cell) I would like to take your attention to the answer of this question. how to add ellipse button and textbox in current cell of datagridview in winforms

I could enhance this solution to see the button control in the cell for all the time. What I want is to get the popup box for the first click of the cell. This is the code to paint the button in uneditted mode.

 // 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);
                    }
Community
  • 1
  • 1
madufit1
  • 206
  • 3
  • 10

1 Answers1

0

A better option would be to embed a button on your DataGridView. This would give you more control over the use of DataGridView. See the following snippet:

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;
}

In the above snippet the location of ellipses button is set to last clicked cell. The visibility is always true after a cell is clicked. In my opinion this would provide a far better control over the button's function,and is easier to maintain.

Nilay Vishwakarma
  • 3,105
  • 1
  • 27
  • 48