如何使用Visual Studio 2012在C#中向Data GridView添加工具提示?
我想把它放在标题单元格中 我在HeaderStyle中看不到任何地方。
我该怎么做?
答案 0 :(得分:2)
特定单元格
您可以向DataGridViewCell
使用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";