views:

101

answers:

5

In C# (VS2005), how can I have extra information that pops out from a cell in a datagrid?

In one column of the grid, there are "YES" or "NO" values, but for the "NO" values I need to offer an explanation for why it is "NO". Is there something obvious that can do this?

+1  A: 

Have you tried binding a tooltip (conditionally) to the cell?

Set the tooltip data in ItemDataBound (or dynamic <%# binding)

nonnb
Will the cell show that there is a tooltip available?
Craig Johnston
No it won't, however you can do that too by adding a CssClass to the cell. i.e. CssClass="has-tooltip". Then use your creativity and add a little "i" icon to indicate there's more information.
Marko
@Marko: isn't this basically a Smart Tag?
Craig Johnston
Gah I confused it with ASP.NET's GridView. Disregard my CssClass comment
Marko
The problem with ToolTip is that if you click on the cell the tip won't display.
Craig Johnston
What about Context Menu? A little ugly but you could insert an item that will be your explination. You will need to add the code to your CellContentClick event. Just have a switch on which mouse-button was clicked. Also, you can dynamically color data grid cells programatically. http://stackoverflow.com/questions/995327/programmatically-change-individual-cell-borders-in-a-winforms-datagridview
Ryan Bennett
+1  A: 

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.

Jesse
Where is the StatusStrip displayed?
Craig Johnston
You choose where to place it, however, StatusStrips are usually docked to the bottom of the Form.
Jesse
A: 

What you want to do is a bind a ToolTip to the individual cells.

The following article from Microsoft should help you get there: http://msdn.microsoft.com/en-us/library/2249cf0a%28VS.85%29.aspx

Hope this helps!

RevolXadda
A: 

Have you tried the ASP.Net AJAX PopupControl? The info is here -> http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/PopupControl/PopupControl.aspx. You can pop up on any control and the popup is as easy as placing controls inside a panel such as labels.

Raymund
A: 

Try using RowDetails; you can specify RowDetailsTemplate to display detailed information about the row. You can see an example of Row Details here.

beingryu