Normally we can set a tooltip for column header in the gridview during the mouse over using ColumnHeaderTooltip. If there is a requirement of setting a tooltip to column cells depending on the data, it is somewhat tedious. Here is an example of setting a tooltip to gridview Column during mouse over.
Consider a column marital status in a grid view. If the Marital Status is ‘M’ then it will set the tool tip “Client is Married!” and change the text color to red during the mouse over.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
}
}
private void LoadData()
{
SqlConnection con;
con = new SqlConnection("server=user11\\user112008;uid=sa;password=acsadmin;database=AdventureWorksDW");
con.Open();
SqlDataAdapter oAdapter = new SqlDataAdapter("SalesReason_S", con);
oAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet oDataSet = new DataSet();
oAdapter.Fill(oDataSet);
GridView1.DataSource = oDataSet.Tables[0];
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow pr = ((DataRowView)e.Row.DataItem).Row;
if (pr["MaritalStatus"].ToString() == "M")
{
e.Row.ForeColor = System.Drawing.Color.Red;
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#f7fff8';");
e.Row.ToolTip = "Client is Married!";
}
}
}
}
In the above code the RowDataBound event we will check the Marital Status is ‘M’ or no, the Row.ToolTip property set the tool tip and Row.Attributes change the text color to red.