How To Add Title Attribute To GridView Row

Let us suppose that you have a :Grid View with about seven rows across.One of the cells is a five-digit code, for a long name.and your requirement is that the user move the mouse over the code cell, and display this long name in a small popup window, to remind the user of which the code describes.Here is a simple solution without using JavaScript. Just add "Title" attribute to each row to display the information. It will have the same effect as mouse over and mouse out

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Mouseover.aspx.cs"
Inherits="Mouseover" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
<title>Untitled Page</title>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server" OnRowDataBound="GridView1_RowDataBound">
<asp:BoundField DataField="id" HeaderText="ProductId" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="sellPrice" HeaderText="SalePrice" />
<asp:TemplateField HeaderText="LongName">
<asp:Label ID="LongName" runat="server" Text='<%#ShortDesc(Eval("Desc").ToString()) %>'></asp:Label>
<asp:TemplateField HeaderText="LongAgencyName" Visible="False">
<asp:Label ID="LongName1" runat="server" Text='<%#Eval("Desc")%>'></asp:Label>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Mouseover : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
GridView1.DataSource = GetData();

public DataSet GetData()
DataSet ds = new DataSet();
DataTable dt = new DataTable("Movie");
DataRow dr;
dt.Columns.Add(new DataColumn("Id", typeof(Int32)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("SellPrice", typeof(Int32)));
dt.Columns.Add(new DataColumn("Desc", typeof(string)));
for (int i = 1; i <= 10; i++)
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Name" + i.ToString();
dr[2] = (i * 3)+i*i;
dr[3] = "Abcdefghijklmnopqwrstuvwxyz"+i.ToString();
Session["dt"] = dt;
return ds;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
Label lbl = (Label)e.Row.FindControl("LongName1");
e.Row.Attributes.Add("Title", lbl.Text);
e.Row.Attributes["style"] = "Cursor:hand";

public string ShortDesc(string strDesc)

return strDesc.Substring(0, 5);


No comments:

Post a Comment