3

I am working on a Windows application in C# using a DataGridView. How do I add a help text to appear when the user hovers over the columns?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gainster
  • 5,481
  • 19
  • 61
  • 90

1 Answers1

3

Use the ToolTip property of DataGridView.

A very good article is How to: Add ToolTips to Individual Cells in a Windows Forms DataGridView Control. It is what you want. Following is sample code.

// Sets the ToolTip text for cells in the Rating column.
void dataGridView1_CellFormatting(object sender,
    DataGridViewCellFormattingEventArgs e)
{
    if ( (e.ColumnIndex == this.dataGridView1.Columns["Rating"].Index)
        && e.Value != null )
    {
        DataGridViewCell cell =
            this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
        if (e.Value.Equals("*"))
        {
            cell.ToolTipText = "very bad";
        }
        else if (e.Value.Equals("**"))
        {
            cell.ToolTipText = "bad";
        }
        else if (e.Value.Equals("***"))
        {
            cell.ToolTipText = "good";
        }
        else if (e.Value.Equals("****"))
        {
            cell.ToolTipText = "very good";
        }
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amit
  • 21,570
  • 27
  • 74
  • 94