WinForms DataGridView Button Column, hide based on condition

Published on : Mar 15, 2009

Category : General

Saravana

Author

You might be wondering what I?m doing with winforms, recently in one of my internal project I had to use winforms for some tooling. I used the data grid that came out of the box which contained a button column. In data grid, its not actually a button, its just painted to look like a button. So, if you want to do some normal button activities like setting the visibility, enabling/disabling etc its not trivial task (I couldn?t find anything simple).
You can download this article as a PDF document Download now.
I just managed to crack this code, which basically hides the button based on the condition. In my case if the one of other column value is less than 1 I need to hide the button. The code is simple to follow, here are the high level steps 1. Attach the cell painting event handler to grid view 2. Check if the current painting column is the button column 3. Get the value from the other column, based on which you need to hide the button. 4. I?m simply drawing the grid lines and erasing the content.
   1: private void dataGridView_ArticlesMgmt_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
   2:         {
   3:             if (this.dataGridView_ArticlesMgmt.Columns["actionColumn"].Index ==
   4:                 e.ColumnIndex && e.RowIndex >= 0)
   5:             {
   6:
   7:                 int articleCount = (int)dataGridView_ArticlesMgmt[dataGridView_ArticlesMgmt.Columns["articleCountColumn"].Index, e.RowIndex].Value;
   8:
   9:                 //If there are no articles to download, then hide the button
  10:                 if (articleCount < 1)
  11:                 {
  12:                     Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
  13:                         e.CellBounds.Y + 1, e.CellBounds.Width - 4,
  14:                         e.CellBounds.Height - 4);
  15:
  16:                     using (
  17:                         Brush gridBrush = new SolidBrush(this.dataGridView_ArticlesMgmt.GridColor),
  18:                         backColorBrush = new SolidBrush(e.CellStyle.BackColor))
  19:                     {
  20:                         using (Pen gridLinePen = new Pen(gridBrush))
  21:                         {
  22:                             // Erase the cell.
  23:                             e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
  24:
  25:                             // Draw the grid lines (only the right and bottom lines;
  26:                             // DataGridView takes care of the others).
  27:                             e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
  28:                                 e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
  29:                                 e.CellBounds.Bottom - 1);
  30:                             e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
  31:                                 e.CellBounds.Top, e.CellBounds.Right - 1,
  32:                                 e.CellBounds.Bottom);
  33:
  34:                             e.Handled = true;
  35:                         }
  36:                     }
  37:                 }
  38:             }
  39:         }
Nandri! Saravana