Click here to Skip to main content
Licence CPOL
First Posted 23 Nov 2005
Views 29,008
Bookmarked 27 times

Datagrid Cell Tooltip

By | 25 Sep 2009 | Article
Showing Tooltip for each cell with data of a table in a Datagrid
 
Part of The SQL Zone sponsored by
See Also

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

    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.

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)

About the Author

Thiru Sagadevan

Team Leader

India India

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 3 PinmemberGeorge#96:26 23 Aug '11  
GeneralMy vote of 1 PinmvpDave Kreskowiak10:18 2 Oct '09  
GeneralMy vote of 1 PinmemberNasir Razzaq0:08 7 Aug '09  
GeneralMy vote of 1 Pinmembersudhir_Kumar1:53 21 Jan '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 25 Sep 2009
Article Copyright 2005 by Thiru Sagadevan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid