开发者

How do you override the default tooltiptext (cell is too small to display value) using the CellToolTipTextNeeded event in a datagridview?

I have a datagridview that requires certain columns to display 开发者_JAVA百科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);
        }
    }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜