gpt4 book ai didi

c# - 在 DataGrid 的单元格上添加额外信息作为弹出窗口

转载 作者:太空狗 更新时间:2023-10-29 20:42:21 25 4
gpt4 key购买 nike

我如何获得从 DataGrid 中的单元格弹出的额外信息?

在网格的一列中,有一个YESNO 值。对于 NO 值,我需要解释为什么它是 NO。有什么简单/明显的方法可以做到这一点吗?

最佳答案

您始终可以使用 StatusStrip 并使用 CellMouseEnterCellMouseLeave 事件设置和删除(分别)状态条中的说明。

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

作为一项附加功能,您可以像 Excel 那样通过显示一个小标记来表明该单元格具有“额外”信息。这是我用来做完全相同事情的一小段代码:

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

如果 hasTooltip 为真,此代码在单元格周围绘制蓝​​色边框,如果 hasCompleted 为真,则绘制绿色边框,如果两者都是正确的。

关于c# - 在 DataGrid 的单元格上添加额外信息作为弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3433314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com