如何使用 datagridview 中的 CellToolTipTextNeeded 事件覆盖默认的工具提示文本(单元格太小而无法显示值)?

发布于 2024-11-14 18:29:43 字数 1515 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌若浮生 2024-11-21 18:29:43

确保您使用的是 CellToolTipTextNeeded 事件的 ToolTipText< /a> 属性来设置单元格的工具提示文本。当控件显示单元格的工具提示时,此设置会覆盖默认显示的文本,这些单元格的大小使它们无法完全显示其值。

尝试我所做的来看到这一点。向 DGV 加载适合所有 DGV 单元格的数据。验证是否未显示任何单元格工具提示。调整列的大小,以便不显示某些单元格的文本,并验证该单元格的值是否显示在单元格的工具提示中。现在将以下代码添加到 DGV 的 CellToolTipTextNeeded 事件中:

e.ToolTipText = string.Format("Hello, I'm on row '{0}', column '{1}'", e.RowIndex, e.ColumnIndex);

重复上述测试。您会注意到,显示了自定义工具提示文本,但不显示显示调整大小的单元格文本的默认工具提示。

还要记住 CellToolTipTextNeeded 事件的 RowIndex 和 ColumnIndex 属性(如上所示);您可能会发现这些在检索工具提示文本所需的文本或确定是否要显示工具提示时很方便。

更新
我根据您发布的代码编写了一个示例。在干净的 Winform (3.5) 中放置一个 Button 和一个 TabControl(将其命名为 tabs),并在表单中使用以下代码:

using System;
using System.Data;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            PopulateTabs();
        }

        private DataSet GetDataSet() {
            DataSet ds = new DataSet();
            var dt1 = new DataTable("dt 1");
            dt1.Columns.Add("dt1 Field1");
            dt1.Columns.Add("dt1 Field2");
            dt1.Rows.Add(new object[] { "dt1f1r1", "dt1f2r1" });
            dt1.Rows.Add(new object[] { "dt1f1r2", "dt1f2r2" });
            dt1.Rows.Add(new object[] { "dt1f1r3", "dt1f2r3" });

            var dt2 = new DataTable("dt 2");
            dt2.Columns.Add("dt2 Field1");
            dt2.Columns.Add("dt2 Field2");
            dt2.Columns.Add("dt2 Field3");
            dt2.Rows.Add(new object[] { "dt2f1r1", "dt2f2r1", "dt2f3r1" });
            dt2.Rows.Add(new object[] { "dt2f1r2", "dt2f2r2", "dt2f3r2" });

            ds.Tables.Add(dt1);
            ds.Tables.Add(dt2);

            return ds;
        }

        private void PopulateTabs() {
            tabs.Visible = true;
            tabs.TabPages.Clear();
            DataSet results_ = GetDataSet();

            foreach (DataTable dt in results_.Tables) {
                if (dt.Rows.Count == 0)
                    continue;
                TabPage 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);
            }
        }

        void dgv_SelectionChanged(object sender, EventArgs e) {
            //throw new NotImplementedException();
        }
        void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
            //throw new NotImplementedException();
        }
        void dgv_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e) {
            e.ToolTipText = string.Format("Hello, I'm on row '{0}', column '{1}'", e.RowIndex, e.ColumnIndex);
        }
    }
}

上面的代码对我有用 - 仅显示我明确设置的工具提示文本。如果上面的演示适合您,请尝试禁用实际代码的某些部分,直到您了解我所拥有的内容,然后开始重新编译,直到看到问题。

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:

e.ToolTipText = string.Format("Hello, I'm on row '{0}', column '{1}'", e.RowIndex, e.ColumnIndex);

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:

using System;
using System.Data;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            PopulateTabs();
        }

        private DataSet GetDataSet() {
            DataSet ds = new DataSet();
            var dt1 = new DataTable("dt 1");
            dt1.Columns.Add("dt1 Field1");
            dt1.Columns.Add("dt1 Field2");
            dt1.Rows.Add(new object[] { "dt1f1r1", "dt1f2r1" });
            dt1.Rows.Add(new object[] { "dt1f1r2", "dt1f2r2" });
            dt1.Rows.Add(new object[] { "dt1f1r3", "dt1f2r3" });

            var dt2 = new DataTable("dt 2");
            dt2.Columns.Add("dt2 Field1");
            dt2.Columns.Add("dt2 Field2");
            dt2.Columns.Add("dt2 Field3");
            dt2.Rows.Add(new object[] { "dt2f1r1", "dt2f2r1", "dt2f3r1" });
            dt2.Rows.Add(new object[] { "dt2f1r2", "dt2f2r2", "dt2f3r2" });

            ds.Tables.Add(dt1);
            ds.Tables.Add(dt2);

            return ds;
        }

        private void PopulateTabs() {
            tabs.Visible = true;
            tabs.TabPages.Clear();
            DataSet results_ = GetDataSet();

            foreach (DataTable dt in results_.Tables) {
                if (dt.Rows.Count == 0)
                    continue;
                TabPage 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);
            }
        }

        void dgv_SelectionChanged(object sender, EventArgs e) {
            //throw new NotImplementedException();
        }
        void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
            //throw new NotImplementedException();
        }
        void dgv_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e) {
            e.ToolTipText = string.Format("Hello, I'm on row '{0}', column '{1}'", e.RowIndex, e.ColumnIndex);
        }
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文