惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
Last Week in AI
Last Week in AI
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
V
Visual Studio Blog
宝玉的分享
宝玉的分享
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
博客园 - 聂微东
S
SegmentFault 最新的问题
博客园 - 司徒正美
罗磊的独立博客
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
美团技术团队
小众软件
小众软件
Jina AI
Jina AI

博客园 - xujh

C# 同步两个子目录文件 ASP.NET中突破上传4M文件的限制 【转】一种在SQLServer中实现Sequence的高效方法 DevExpress的ASPxGridView中实现Master-Detail数据动态绑定 【转】浅谈:国内软件公司为何无法做大做强? - xujh - 博客园 一个正则表达式测试(只可输入中文、字母和数字) - xujh - 博客园 IE6、7下如何设置页面内部件高度和窗口等高(heihgt:100%) HP大中华区总裁孙振耀退休感言 C#的一个双缓冲显示的例子 【ASP.NET】防止ASP.NET按钮多次提交的办法 【ASP.NET】FCKeditor 2.6 + Asp.Net 设置 【ASP.NET】FreeTextBox的使用方法 在VS2005下配置手机设备仿真器访问局域网 [Delphi] 截屏存盘 C# 画圆角矩形 有意思的加菲猫语录 [Delphi]用Delphi7访问.NET 2.0的WebService 转贴[ASP.NET]基于Forms认证的WebService应用 ASP.NET优秀博客搜集
[转贴]DataGrid中的高级ToolTip
xujh · 2006-04-07 · via 博客园 - xujh

转自:http://blog.csdn.net/yx10/archive/2005/07/18/427766.aspx
效果如下图所示:

  实现原理:

     为DataGrid中的每一行,绑定onmouseover、onmousemove、onmouseout事件,使的鼠标移动到行内时,自动显示一个<div>,鼠标移出该行,就把这个<div>隐藏掉。

实现代码:
     前台:
1.   定义<div>的样式:
<style type="text/css">
.transparent { FILTER: alpha(opacity=85);
BORDER-TOP: indianred 1px solid;
BORDER-RIGHT: indianred 1px solid; 
BORDER-LEFT: indianred 1px solid;
BORDER-BOTTOM: indianred 1px solid;
POSITION: absolute;
BACKGROUND-COLOR: infobackground;
DISPLAY: none  }
</style>
2.   显示和隐藏窗体的脚本:
<script language="javascript">
     //显示弹出窗体
     function Show(Country, City, Address, PostalCode, Phone, Fax)
     {
         document.getElementById("td1").innerText="国家:"+Country;
         document.getElementById("td2").innerText="城市:"+City;
         document.getElementById("td3").innerText="地址:"+Address;
         document.getElementById("td4").innerText="邮政编码:"+PostalCode;
         document.getElementById("td5").innerText="电话:"+Phone;
         document.getElementById("td6").innerText="传真:"+Fax;
         //获得鼠标的X轴的坐标
         x = event.clientX + document.body.scrollLeft;
         //获得鼠标的Y轴的坐标
         y = event.clientY + document.body.scrollTop + 20;
         //显示弹出窗体
         Popup.style.display="block";
         //设置窗体的X,Y轴的坐标
         Popup.style.left = x;
         Popup.style.top = y;
     }
     //隐藏弹出窗体
     function Hide()
     {
         //隐藏窗体
         Popup.style.display="none";
     }
</script>
3.   ToolTip窗体的代码
<div id="Popup" class="transparent" style=" Z-INDEX: 200">
     <table border="0" cellpadding="0" style="FONT-SIZE: x-small">
         <tr>
           <td align="middle" bgcolor="indianred"><font color="white">联系方式</font></td>
         </tr>
         <tr><td id="td1"></td></tr>
         <tr><td id="td2"></td></tr>
         <tr><td id="td3"></td></tr>
         <tr><td id="td4"></td></tr>
         <tr><td id="td5"></td></tr>
         <tr><td id="td6"></td></tr>
     </table>
</div>
后台:
private DataTable dt;
private void Page_Load(object sender, System.EventArgs e)
{
     // Put user code to initialize the page here
     if(!IsPostBack)
     {
          SqlConnection cnn = new SqlConnection();
          cnn.ConnectionString = "data source=localhost;initial           catalog=Northwind;password=;"
         +"persist security info=True;user id=sa;workstation id=APJ062;packet size=4096";
         string sqlstr = "select Top 16 CustomerID, CompanyName, ContactTitle,Country,          City, Address,PostalCode,Phone,Fax from Customers";
         cnn.Open();
         SqlDataAdapter ad = new SqlDataAdapter(sqlstr,cnn);
         dt = new DataTable();
         ad.Fill(dt);
         grdCustomer.DataSource = dt;
         grdCustomer.DataBind();
     }
}
private void grdCustomer_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
     if(e.Item.ItemType == ListItemType.AlternatingItem
         || e.Item.ItemType == ListItemType.Item)
     {
         e.Item.Attributes.Add("onmouseover",
         "this.oldcolor=this.style.backgroundColor;this.style.backgroundColor='#C8F7FF';");
         e.Item.Attributes.Add("onmousemove",
         "Show('"+dt.Rows[e.Item.ItemIndex]["country"].ToString()+"','"
          +dt.Rows[e.Item.ItemIndex]["City"].ToString()+"','"
          +dt.Rows[e.Item.ItemIndex]["Address"].ToString()+"','"
          +dt.Rows[e.Item.ItemIndex]["PostalCode"].ToString()+"','"
          +dt.Rows[e.Item.ItemIndex]["Phone"].ToString()+"','"
          +dt.Rows[e.Item.ItemIndex]["Fax"].ToString()+"');");
          e.Item.Attributes.Add("onmouseout",
          "this.style.backgroundColor=this.oldcolor;Hide();");
     }
}