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?
newbiewebSr. Software EngineerAsked:
Who is Participating?
 
Robert SchuttConnect With a Mentor Software 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
 
Robert SchuttConnect With a Mentor Software 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
 
newbiewebSr. Software EngineerAuthor 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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Robert SchuttConnect With a Mentor Software 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
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
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
 
newbiewebSr. Software EngineerAuthor Commented:
I agree. Mass producing tool tips is a very bad idea.

Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.