• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1281
  • Last Modified:

C# WinForms: DataGridView tool tip does not display on click

I create the tool tip (for the row) when the user clicks any column on the row of the data which makes up the tool tip.  But the user must move the mouse away then back to the row for the tool tip to display.

Is there any way I can force Windows to display the tool tip immediately?
0
newbieweb
Asked:
newbieweb
  • 3
  • 2
4 Solutions
 
Robert SchuttSoftware EngineerCommented:
I think it may be possible but to get to the tooltip within the datagridview you may need to make a custom datagridview component.

I would suggest however to try a different approach first. Is it really not possible to create the tooltip earlier? After your previous question about this I did a little test and made a function to create a "row tooltip" which is updated automatically:
private void Form1_Load(object sender, EventArgs e) {
    this.myDataGridView.ShowCellToolTips = true;
    this.myDataGridView.RowsAdded += dgv_RowsAdded;
    this.myDataGridView.CellValueChanged += dgv_CellValueChanged;
}

private void dgv_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) {
    for (int r = 0; r < e.RowCount; r++) {
        setRowTT(e.RowIndex + r);
    }
}

private void dgv_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
    if (e.RowIndex > -1) {
        setRowTT(e.RowIndex);
    }
}

private void setRowTT(int rowIndex) {
    string tt = string.Format("Row {0}: ", rowIndex + 1);
    for (int c = 0; c < this.myDataGridView.Columns.Count; c++) {
        tt += string.Format("\n{0}: '{1}'", this.myDataGridView.Columns[c].HeaderText, this.myDataGridView.Rows[rowIndex].Cells[c].Value);
    }
    this.myDataGridView.Rows[rowIndex].HeaderCell.ToolTipText = tt;
    for (int c = 0; c < this.myDataGridView.Columns.Count; c++) {
        this.myDataGridView.Rows[rowIndex].Cells[c].ToolTipText = tt;
    }
}

Open in new window

0
 
newbiewebAuthor Commented:
I do not want the processsor load of creating tool tips for an ever increasing number of rows in the DataGridView.  I could get fancy and make sure that when a row becomes visible, I create the Tool Tip.  Isn't there an easier way?
0
 
Robert SchuttSoftware EngineerCommented:
Have you looked into making a custom component derived from the DataGridView?

Another thought is making a label designed to look like a tooltip and fill/position/show that when a row is clicked.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Robert SchuttSoftware EngineerCommented:
I think I found a better way:
        int SavX = -1;
        int SavY = -1;
        private void dgv_MouseMove(object sender, EventArgs e) {
            if (this.myDataGridView.SelectedCells.Count > 0 && (MousePosition.X != SavX || MousePosition.Y != SavY)) {
                SavX = MousePosition.X;
                SavY = MousePosition.Y;
                this.addSpecialtyToolTip.SetToolTip(this.myDataGridView, getRowTT(this.myDataGridView.SelectedCells[0].RowIndex));
            }
        }
        private string getRowTT(int rowIndex) {
            string tt = string.Format("Row {0}: ", rowIndex + 1);
            for (int c = 0; c < this.myDataGridView.Columns.Count; c++) {
                tt += string.Format("{2}{0}: '{1}'", this.myDataGridView.Columns[c].HeaderText, this.myDataGridView.Rows[rowIndex].Cells[c].Value, c == 0 ? "" : ", ");
            }
            return tt;
        }

Open in new window

in the Form_Load add:
this.myDataGridView.ShowCellToolTips = false; // use own tooltip
this.myDataGridView.MouseMove += dgv_MouseMove;

Open in new window

0
 
AndyAinscowCommented:
Just a small aside point:

>>I do not want the processsor load of creating tool tips for an ever increasing number of rows in the DataGridView.  I could get fancy and make sure that when a row becomes visible, I create the Tool Tip.  Isn't there an easier way?

You should never have to do that.  Usually a Tooltip takes existing text or you create the text when the tip requests it.  Doing what you said is very inefficient - what if a tip is never required, you may have created and stored hundreds of tips in some sort of collection.
0
 
newbiewebAuthor Commented:
I agree. Mass producing tool tips is a very bad idea.

Thanks.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now