Click here to Skip to main content
15,885,653 members
Articles / Web Development / ASP.NET

Datagrid Cell Tooltip

Rate me:
Please Sign up or sign in to vote.
1.75/5 (7 votes)
25 Sep 2009CPOL 47.6K   28   4
Showing Tooltip for each cell with data of a table in a Datagrid

Introduction

This article is used to display tooltip for each cell in a datagrid with datasource datatable's data in a simplified way. 

To accomplish this, We have to use ItemDataBound() event of the datagrid.  Before we begin with ItemDataBound() event, we need to set datatable for the datagrid which we use to display the data. 

Page_load() Event

C#
private void Page_Load(object sender, System.EventArgs e)
    {
        // Put user code to initialize the page here
        if( !IsPostBack)
        {

            dt = new DataTable();
            dt.Columns.Add("EmpNo",System.Type.GetType("System.Int32"));
            dt.Columns.Add("EmpName",System.Type.GetType("System.String"));
            dt.Columns.Add("EmpSalary",System.Type.GetType("System.Int32"));

            DataRow drow;

            drow = dt.NewRow();
            drow["EmpNo"] = 1001;
            drow["EmpName"] = "Ranjith";
            drow["EmpSalary"] = 40000;
            dt.Rows.Add(drow);

            drow = dt.NewRow();
            drow["EmpNo"] = 1003;
            drow["EmpName"] = "Surrendra";
            drow["EmpSalary"] = 30000;
            dt.Rows.Add(drow);


            drow = dt.NewRow();
            drow["EmpNo"] = 1002;
            drow["EmpName"] = "Thiru";
            drow["EmpSalary"] = 20000;
            dt.Rows.Add(drow);

            drow = dt.NewRow();
            drow["EmpNo"] = 1001;
            drow["EmpName"] = "Renuga";
            drow["EmpSalary"] = 10000;
            dt.Rows.Add(drow);

            DataGrid1.DataSource = dt.DefaultView;
            DataGrid1.DataBind();
        }
    }

ItemDataBound() Event

The ItemDataBound() Event gets fired when data from datatable binds with datagrid. This is where we add tooltip for each and every cell of the datagrid.

C#
private void DataGrid1_ItemDataBound(object sender, 
        System.Web.UI.WebControls.DataGridItemEventArgs e)
{
    if((e.Item.ItemType==ListItemType.AlternatingItem) || 
        (e.Item.ItemType==ListItemType.Item))
    {
        for(int i=0;i<e.Item.Cells.Count;i++)
        {
            e.Item.Cells[i].ToolTip= e.Item.Cells[i].Text;
        }
    }
}

Conclusion

The ItemDataBound event can be used to do anything which we want to do while datagrid data get bound. I hope this article will be useful for you all.

License

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


Written By
Team Leader
India India
I am working as Team Lead in Chennai(India) based software development company. I love working in .Net languages like VB.net and C#.net both in web and windows applications.

Comments and Discussions

 
GeneralMy vote of 3 Pin
George#923-Aug-11 6:26
George#923-Aug-11 6:26 
GeneralMy vote of 1 Pin
Dave Kreskowiak2-Oct-09 10:18
mveDave Kreskowiak2-Oct-09 10:18 
GeneralMy vote of 1 Pin
Nasir Razzaq7-Aug-09 0:08
Nasir Razzaq7-Aug-09 0:08 
GeneralMy vote of 1 Pin
sudhir_Kumar21-Jan-09 1:53
sudhir_Kumar21-Jan-09 1:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.