Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

how to show the tooltip for gridview header columns using asp.net

May 28, 2011
Introduction:

Here I will explain how to show the tooltip for gridview header columns using asp.net.


Description:

In previous article I explained clearly how to show tooltip for gridview rows and columns and in another post I explained clearly show up and down arrows for gridview sorting . Now I will explain how to show tooltip for gridview headers using asp.net with slight code modification using above posts. To display tooltip for gridview header first design your aspx code like this


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Show Tooltip for Gridview Header</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="gvdetails" DataSourceID="dsdetails" AllowPaging="true" AllowSorting="true" AutoGenerateColumns="false" onrowdatabound="gvdetails_RowDataBound">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="UserId" HeaderText="UserId" SortExpression="UserId" />
<asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="dsdetails" runat="server" ConnectionString="<%$ConnectionStrings:dbconnection %>"
SelectCommand="select * from UserInformation"/>
</div>
</form>
</body>
</html>
Here don’t forgot to set the connection string in web.config file here I am getting database connection from web.config file for that reason you need to set the connectionstring in web.config file like this

<connectionStrings>
<add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"/>
</connectionStrings>

In code behind gridview RowDataBound event write following code to display tooltip for gridview headers.

C#.NET Code


protected void gvdetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
//This condition is used to check RowType is Header
if(e.Row.RowType==DataControlRowType.Header)
{
for (int i = 0; i < gvdetails.Columns.Count; i++)
{
e.Row.Cells[i].ToolTip = gvdetails.Columns[i].HeaderText;
}
}
}

VB.NET Code


  Protected Sub gvdetails_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        'This condition is used to check RowType is Header
        If e.Row.RowType = DataControlRowType.Header Then
            For i As Integer = 0 To gvdetails.Columns.Count - 1
                e.Row.Cells(i).ToolTip = gvdetails.Columns(i).HeaderText
            Next
        End If
    End Sub
Demo

If you observe codebehind code I am displaying gridview tooltips based HeaderText if you want to display tooltip based on SortExpression then change this line

e.Row.Cells[i].ToolTip = gvdetails.Columns[i].HeaderText;

To

e.Row.Cells[i].ToolTip = gvdetails.Columns[i].SortExpression;


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

6 comments :

Anonymous said...

good

BA said...

The far easier way to do this is by using a TemplateField instead of a BoundField.

BA said...

see this page for an example

BA said...

ugh, annoying, my link didn't get posted. Let's try this again, here's the site:
http://forums.asp.net/t/1102549.aspx/1

Also your link at the top "how to show tooltip for gridview rows and columns" is going to the wrong page.

Anonymous said...

its very helpful for freshers to asp.net

Anonymous said...

i tried.its vely helpful. Great.good post. Thank you.

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.