Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add extra information as a pop out on a cell of a DataGrid

How can I have extra information that pops out from a cell in a DataGrid?

In a column of the grid, there is a YES or NO value. For the NO values, I need to offer an explanation for why it is NO. Is there something simple/obvious that can do this?

like image 454
CJ7 Avatar asked Aug 08 '10 06:08

CJ7


1 Answers

You can always have a StatusStrip and using the CellMouseEnter and CellMouseLeave events set and remove (respectively) the explanation from the status strip.

  private void dgvCellMouseEnter(object sender, DataGridViewCellEventArgs e)
  {
      statusStrip1.Text = (sender as DataGridView)[e.ColumnIndex, e.RowIndex].ToolTipText;
  }

  private void dgvCellMouseLeave(object sender, DataGridViewCellEventArgs e)
  {
      statusStrip1.Text = "";
  }

As an added feature, you can show that the cell has "extra" info by showing a small mark such as Excel does. Here's a small snippet of code that I use to do the exact same thing:

  private void dgvCellPainting(object sender, DataGridViewCellPaintingEventArgs e)
  {
      if (e.ColumnIndex != -1) && (e.RowIndex != -1)
      {
          DataGridViewCell dgvCell = (sender as DataGridView)[e.ColumnIndex, e.RowIndex];

          Pen greenPen = new Pen(Color.Green, 2);
          Boolean hasTooltip = !dgvCell.ToolTipText.Equals("");
          Boolean hasCompleted = (dgvCell.Tag as CellInfo).complete; // CellInfo is a custom class

          if (hasTooltip) && (hasCompleted)
          {
              e.Handled = true;
              e.Paint(e.ClipBounds, e.PaintParts);
              e.Graphics.DrawRectangle(Pens.Blue, e.CellBounds.Left + 5, e.CellBounds.Top + 2, e.CellBounds.Width - 12, e.CellBounds.Height - 6);
              e.Graphics.DrawRectangle(greenPen, e.CellBounds.Left + 1, e.CellBounds.Top + 1, e.CellBounds.Width - 3, e.CellBounds.Height - 3);
          }
          else if (hasTooltip)
          {
              e.Handled = true;
              e.Paint(e.ClipBounds, e.PaintParts);
              e.Graphics.DrawRectangle(Pens.Blue, e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width - 2, e.CellBounds.Height - 2);
          }
          else if (hasCompleted)
          {
              e.Handled = true;
              e.Paint(e.ClipBounds, e.PaintParts);
              e.Graphics.DrawRectangle(greenPen, e.CellBounds.Left + 1, e.CellBounds.Top + 1, e.CellBounds.Width - 3, e.CellBounds.Height - 3);
          }
      }
  }

This code draws a blue border around the cell if hasTooltip is true, a green border if hasCompleted is true, and both borders (with the green one inside) if both are true.

like image 57
Jesse Avatar answered Oct 21 '22 04:10

Jesse