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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With