Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to Display TOOL TIP in Gridview Column.
====================================


am working on asp.net c# sqlServer2005.

i have large amount of data in database i have displayed in gridview its fine.

i have one column in Gridview with name Days. In this column i have M T W Th F S Su 0.

so when the user keeps the mouse pointer on M it should Monday and
T - Tuesday
W- Wednesday
Th-Thursday
F - Friday
S- Saturday
Su - Sunday
0 - Not Available.

Please can u help me how to set tooltip for this column.

In one column i should display 8 tool tips.

Please help Thanks.
Posted

Try this

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var MONTH_COLUMN_SELL = 8; // Please change your month column cell here
            e.Row.Cells[MONTH_COLUMN_SELL].ToolTip = getMonthName(e.Row.Cells[MONTH_COLUMN_SELL].Text);
        }
    }

    private string getMonthName(string monthCode)
    {
        string monthName = string.Empty;
        switch (monthCode)
        {
            case "M":
                monthName="Monday";
                break;

            // add other condition as you like
        }
        return monthName;
    }


Here is another method to do this, you have to modify your grid view binding way using Template field

ASP.NET
<asp:TemplateField HeaderText="ClearanceNumber">
 <ItemTemplate>
      <asp:Label ID="lblClearanceNumber" runat="server"
                Text='<%# Eval("ClearanceNumber") %>' 
                Tooltip='<%# Eval("ClearanceNumber") %>'>
      </asp:Label>      
 </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Days">
 <ItemTemplate>
      <asp:Label ID="lblDays" runat="server"
                Text='<%# Eval("Days") %>' 
                Tooltip='<%# getMonthName(Eval("Days")) %>'>
      </asp:Label>      
 </ItemTemplate>
</asp:TemplateField>


In code behind

C#
protected string getMonthName(string monthCode)
    {
        string monthName = string.Empty;
        switch (monthCode)
        {
            case "M":
                monthName = "Monday";
                break;

            case "T":
                monthName = "Tuesday";
                break;

            case "W":
                monthName = "Wednesday";
                break;

            case "Th":
                monthName = "Thursday";
                break;

            case "F":
                monthName = "Friday";
                break;

            case "S":
                monthName = "Saturday";
                break;

            case "Su":
                monthName = "Sunday";
                break;

            // add other condition as you like
        }
        return monthName;
    }


please check this method scope is protected
 
Share this answer
 
v4
Comments
Ranjith Reddy CSE 12-Jan-13 5:23am    
my cell is 8....please help
Tharaka MTR 12-Jan-13 5:28am    
ok. I have modified the code, you have to modify the switch statement with other condition such as "T" - Tuesday, "W" - Wednesday etc..

Please try and let me know if you need any more help
Ranjith Reddy CSE 12-Jan-13 5:35am    
Sorry Boss, its not working....when i keep mouse pointer...its unable to show. PLEASE HELP.
Tharaka MTR 12-Jan-13 5:38am    
could you please show me your code behind file and grid view layout..
did you debug the project and check whether the event is firing or not?
Ranjith Reddy CSE 12-Jan-13 5:49am    
This is my code.

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class _Default : System.Web.UI.Page
{
private static readonly string _connString = String.Empty;
SqlConnection con = new SqlConnection(_connString);

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadGridData();
}

}

private void LoadGridData()
{
SqlConnection con = new SqlConnection(_connString);


SqlCommand cmd = new SqlCommand("Select * from Flight", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

GridView1.DataSource = ds;
GridView1.DataBind();
}



protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var MONTH_COLUMN_SELL = 8; // Please change your month column cell here
e.Row.Cells[MONTH_COLUMN_SELL].ToolTip = getMonthName(e.Row.Cells[MONTH_COLUMN_SELL].Text);
}
}

private string getMonthName(string monthCode)
{
string monthName = string.Empty;
switch (monthCode)
{
case "M":
monthName = "Monday";
break;

case "T":
monthName = "Tuesday";
break;

case "W":
monthName = "Wednesday";
break;

case "Th":
monthName = "Thursday";
break;

case "F":
monthName = "Friday";
break;

case "S":
monthName = "Saturday";
break;

case "Su":
monthName = "Sunday";
break;

// add other condition as you like
}
return monthName;
}


static _Default()
{
_connString = WebConfigurationManager.ConnectionStrings["CFS_DBConnectionString"].ConnectionString;
}
}
Hi Ranjith

Choose EditColumns property of DataGridView. First Click on particular column and then go to ToolTipText property on right side pane and finally edit your Text.
 
Share this answer
 
Comments
Member 13574500 16-Mar-18 10:41am    
I have gone to Edit Columns etc. but can't see a ToolTipText anywhere.
Is it in some specific area?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900