Microsoft Dot Net Master

Microsoft Dot Net Master
Microsoft Dot Net Master

Monday, June 20, 2011

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

1: .aspx code


<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>
 
2: connection string in web.config
 
<connectionStrings>
<add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"/>
</connectionStrings>
 
3: In code behind gridview RowDataBound event write following code to display tooltip for gridview headers.
 
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;
}
}
}
  
 



 

No comments:

Post a Comment