How to show tooltip in gridview?


Hi

Here are some few steps to do this fuctionality

Step 1. Take one gridview control

<%@ Page Language=”C#” AutoEventWireup=”true”  CodeFile=”Default.aspx.cs” Inherits=”_Default” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;

<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False”
onrowdatabound=”GridView1_RowDataBound”>
<Columns>
<asp:TemplateField HeaderText=”Id”>
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText=”Description1″  DataField=”Desc1″ >
<ItemStyle Font-Bold=”True” Font-Italic=”True”
ForeColor=”#FF3300″ />
</asp:BoundField>
</Columns>
</asp:GridView>
</div>

</form>
</body>
</html>

Step 2. Click on RowDataBound event of Gridview and write this code in code behind file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(“Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True”);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillGrid();
}

}

private void FillGrid()
{
SqlDataAdapter da = new SqlDataAdapter(“Select *from tblStory”, con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ViewState[“Data”] = e.Row.Cells[1].Text;
if (e.Row.Cells[1].Text.Length > 25)
{
e.Row.Cells[1].Text = e.Row.Cells[1].Text.Substring(0, 25) + “…”;
e.Row.Cells[1].ToolTip = ViewState[“Data”].ToString();
}
}
}
}

Then you will get output like this

Advertisement

2 thoughts on “How to show tooltip in gridview?

  1. Happy February 21, 2012 / 5:21 am

    Hello…

    This information is quite useful

    thanks a lot……………….for posting…..

    • Chandra Dev February 22, 2012 / 2:05 am

      I m glad to know that you liked my artical.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.