Reputation:
How can I add a Tooltip to Data GridView in C# using Visual Studio 2012?
I want to put it in the header cells I don't see any place in HeaderStyle.
how can i do that?
Upvotes: 0
Views: 10928
Reputation: 54453
Each Column
has a Property HeaderCell
of type DataGridViewHeaderCell
.
You can set its ToolTipText
like this:
dataGridView1.Columns[columnIndexOrName].HeaderCell.ToolTipText = "OK";
Upvotes: 1
Reputation: 13578
Specific cell
You can add a tooltip to the DataGridViewCell
Use the CellFormatting event as demonstrated on MSDN:
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";
}
}
}
You may have to set the ShowCellToolTips
property on the datatable to true
;
Entire row
If you want to set a tooltip on the entire header row then use something like this:
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");
}
}
Upvotes: 3