如何使用 datagridview 中的 CellToolTipTextNeeded 事件覆盖默认的工具提示文本(单元格太小而无法显示值)?
我有一个 datagridview 需要某些列来显示自定义工具提示文本。我尝试使用 CellToolTipTextNeeded 事件按需加载工具提示文本,而不是将它们全部存储在前面,但这给了我一些问题。起初,我在初始数据绑定时将它们全部分配,它们按我的预期显示。
现在我已经通过事件加载了工具提示,我必须将鼠标悬停在单元格上两次才能查看自定义工具提示。首先,如果单元格内容太大,要查看 .Net 显示的默认工具提示文本,那么我必须将鼠标悬停在另一个单元格上,然后返回查看我的自定义工具提示。
我尝试将每个单元格的工具提示文本设置为空白,但正如我所想,这不起作用。有什么想法吗?
这是我实现的代码:
private void PopulateTabs()
{
tabs.Visible = true;
tabs.TabPages.Clear();
results_ = some Dataset
foreach (DataTable dt in results_.Tables)
{
if (dt.Rows.Count == 0)
continue;
tab = new TabPage(dt.TableName);
DataGridView dgv = new DataGridView();
dgv.DataSource = dt.DefaultView;
dgv.Name = dt.TableName;
dgv.Dock = DockStyle.Fill;
dgv.SelectionChanged += new EventHandler(dgv_SelectionChanged);
dgv.RowHeadersVisible = false;
if (dt.TableName == Recon.ControlEvalResultsTablename || dt.TableName == Recon.TestEvalResultsTablename)
dgv.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dgv_RowPostPaint);
else
{
dgv.CellToolTipTextNeeded += new DataGridViewCellToolTipTextNeededEventHandler(dgv_CellToolTipTextNeeded);
dgv.CellFormatting += new DataGridViewCellFormattingEventHandler(dgv_CellFormatting);
}
tab.Controls.Add(dgv);
tabs.TabPages.Add(tab);
}
}
I have a datagridview that requires certain columns to display a custom tooltiptext. I'm trying to load the tooltiptext on demand with the CellToolTipTextNeeded event rather than storing them all up front but it's giving me some issues. At first, I assigned them all at initial data binding, and they showed up as I expected.
Now that I have the tooltips load through the event, I have to mouseover a cell twice to see my custom tooltip. First, to see the default tooltiptext that .Net shows if the cell contents are too big, then I have to mouseover another cell and come back to see my custom tooltip.
I tried setting every cell's tooltiptext to blank, but that didn't work, as I figured. Any ideas?
Here is the code where I implement:
private void PopulateTabs()
{
tabs.Visible = true;
tabs.TabPages.Clear();
results_ = some Dataset
foreach (DataTable dt in results_.Tables)
{
if (dt.Rows.Count == 0)
continue;
tab = new TabPage(dt.TableName);
DataGridView dgv = new DataGridView();
dgv.DataSource = dt.DefaultView;
dgv.Name = dt.TableName;
dgv.Dock = DockStyle.Fill;
dgv.SelectionChanged += new EventHandler(dgv_SelectionChanged);
dgv.RowHeadersVisible = false;
if (dt.TableName == Recon.ControlEvalResultsTablename || dt.TableName == Recon.TestEvalResultsTablename)
dgv.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dgv_RowPostPaint);
else
{
dgv.CellToolTipTextNeeded += new DataGridViewCellToolTipTextNeededEventHandler(dgv_CellToolTipTextNeeded);
dgv.CellFormatting += new DataGridViewCellFormattingEventHandler(dgv_CellFormatting);
}
tab.Controls.Add(dgv);
tabs.TabPages.Add(tab);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保您使用的是 CellToolTipTextNeeded 事件的 ToolTipText< /a> 属性来设置单元格的工具提示文本。当控件显示单元格的工具提示时,此设置会覆盖默认显示的文本,这些单元格的大小使它们无法完全显示其值。
尝试我所做的来看到这一点。向 DGV 加载适合所有 DGV 单元格的数据。验证是否未显示任何单元格工具提示。调整列的大小,以便不显示某些单元格的文本,并验证该单元格的值是否显示在单元格的工具提示中。现在将以下代码添加到 DGV 的 CellToolTipTextNeeded 事件中:
重复上述测试。您会注意到,显示了自定义工具提示文本,但不显示显示调整大小的单元格文本的默认工具提示。
还要记住 CellToolTipTextNeeded 事件的 RowIndex 和 ColumnIndex 属性(如上所示);您可能会发现这些在检索工具提示文本所需的文本或确定是否要显示工具提示时很方便。
更新
我根据您发布的代码编写了一个示例。在干净的 Winform (3.5) 中放置一个 Button 和一个 TabControl(将其命名为
tabs
),并在表单中使用以下代码:上面的代码对我有用 - 仅显示我明确设置的工具提示文本。如果上面的演示适合您,请尝试禁用实际代码的某些部分,直到您了解我所拥有的内容,然后开始重新编译,直到看到问题。
Make sure you're using the CellToolTipTextNeeded event's ToolTipText property to set your cells' tool tip text. This setting does override the default text displayed when the control shows tool tips for cells that are sized so they cannot completely display their value.
Try what I did to see this. Load a DGV with data that fits within all DGV cells. Verify that no cell ToolTips are being displayed. Resize a column so some of a cell's text is not displayed and verify that this cell's value appears in the cell's ToolTip. Now add the following code to the DGV's CellToolTipTextNeeded event:
Repeat the tests above. You'll notice that the custom tool tip text is displayed and the default tool tip that displays a resized cell's text is not displayed.
Also keep in mind the CellToolTipTextNeeded event's RowIndex and ColumnIndex properties (demonstrated above); you might find these handy in retrieving the text you'll want for your tool tip text or for determining if you want to display a tool tip at all.
UPDATE
I worked up an example based on the code you posted. Drop a Button and a TabControl (name it
tabs
) in a clean Winform (3.5) and use the following code for the form:The above works for me - only my explicitly set tool tip text appears. If the above demo works for you try disabling sections of your real code until you're down to what I have and then start re-eanbling until you see a problem.