将工具提示添加到Data GridView

时间:2014-07-29 12:48:14

标签: c# visual-studio-2012 datagridview

如何使用Visual Studio 2012在C#中向Data GridView添加工具提示?

我想把它放在标题单元格中 我在HeaderStyle中看不到任何地方。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

特定单元格

您可以向DataGridViewCell

添加工具提示

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.tooltiptext(v=vs.110).aspx

使用MSDN上演示的CellFormatting事件:

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";
        }
    }
}

您可能必须将数据表上的ShowCellToolTips属性设置为true;

整行

如果要在整个标题行上设置工具提示,请使用以下内容:

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // I assumed the header row was the first row, that explains the check for 0, change it if needed.
    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.Index == 0)
    {
        e.Row.ToolTip = "Header tooltip";
         //or (e.g. for web)
        e.Row.Attributes.Add("title", "Header tooltip");
    }
 }

答案 1 :(得分:0)

每个Column都有一个HeaderCell类型的属性DataGridViewHeaderCell

您可以像这样设置ToolTipText

dataGridView1.Columns[columnIndexOrName].HeaderCell.ToolTipText = "OK";