考虑我的 DataGridView,当鼠标悬停在 NameID 字段中的单元格上时,基于单元格中存在的值应该显示工具提示。 例如:如上图所示,当鼠标悬停在 NameID 字段中的值“3”上时 - “ABC”显示为工具提示,类似地,对于“1”,它应该显示“DBC”等等。
下面是我用 C#-Winforms 编写的代码,基于此链接中的文章:https://msdn.microsoft.com/en-us/library/2249cf0a(v=vs.110).aspx
但这似乎不起作用,甚至 ShowCellToolTips 属性也设为 True。
void ToolTip1(object sender,DataGridViewCellFormattingEventArgs e)
{
if ((e.ColumnIndex == this.dataGridView1.Columns["NameID"].Index)
&& e.Value != null)
{
DataGridViewCell cell =
this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (e.Value.Equals("0"))
{
cell.ToolTipText = "Please update NameID as required, To know more click Help icon";
}
else if (e.Value.Equals("1"))
{
cell.ToolTipText = "DBC";
}
else if (e.Value.Equals("2"))
{
cell.ToolTipText = "XYZ";
}
else if (e.Value.Equals("3"))
{
cell.ToolTipText = "ABC";
}
}
}
我怎样才能做到这一点?如何实现?
最佳答案
你可以像这样使用 CellMouseEnter
事件:
private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if ((e.ColumnIndex == this.dataGridView1.Columns["NameID"].Index))
{
//column name
DataGridViewCell cell =
this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
//column id
DataGridViewCell cell1 =
this.dataGridView1.Rows[e.RowIndex].Cells["NameID"];
cell.ToolTipText = "DBC";
if (cell1.Equals("0"))
{
cell.ToolTipText = "Please update NameID as required, To know more click Help icon";
}
else if (cell1.Equals("1"))
{
cell.ToolTipText = "DBC";
}
else if (cell1.Equals("2"))
{
cell.ToolTipText = "XYZ";
}
else if (cell1.Equals("3"))
{
cell.ToolTipText = "ABC";
}
}
}
在这里你可以找到more
关于c# - 当鼠标悬停在单元格上时,如何根据单元格中存在的值显示 DataGridView 中单元格的工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41761697/