Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Forms application: help text on a grid view

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?

like image 376
Gainster Avatar asked Apr 24 '26 16:04

Gainster


1 Answers

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";
        }
    }
}
like image 92
jams Avatar answered Apr 26 '26 05:04

jams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!