DataGrid中的高級ToolTip 2
CS
1
using
System;
2
using
System.Collections;
3
using
System.ComponentModel;
4
using
System.Data;
5
using
System.Data.SqlClient;
6
using
System.Drawing;
7
using
System.Web;
8
using
System.Web.SessionState;
9
using
System.Web.UI;
10
using
System.Web.UI.WebControls;
11
using
System.Web.UI.HtmlControls;
12
using
System.Configuration;
13
14
namespace
CSDNTech
15
{
16 /**//// <summary>
17 /// DataGrid中的高級ToolTip 的摘要說明。
18 /// </summary>
19 public class DataGrid中的高級ToolTip : System.Web.UI.Page
20 {
21 protected System.Web.UI.WebControls.DataGrid DataGrid1;
22 protected string Conn = ConfigurationSettings.AppSettings["DBConn"];
23 private DataTable dt;
24
25 private void Page_Load(object sender, System.EventArgs e)
26 {
27 // 在此處放置用戶代碼以初始化頁面
28 this.Format_DataGrid();
29 }
30
31 private void Format_DataGrid()
32 {
33 SqlConnection cn = new SqlConnection(Conn);
34 cn.Open();
35 try
36 {
37 SqlCommand com = new SqlCommand("select Top 16 CustomerID, CompanyName, ContactTitle,Country, City, Address,PostalCode,Phone,Fax from Customers",cn);
38 SqlDataAdapter adp = new SqlDataAdapter(com);
39 dt = new DataTable();
40 adp.Fill(dt);
41 this.DataGrid1.DataSource = dt;
42 this.DataGrid1.DataBind();
43 }
44 finally
45 {
46 cn.Close();
47 }
48 }
49
50 Web 窗體設計器生成的代碼#region Web 窗體設計器生成的代碼
51 override protected void OnInit(EventArgs e)
52 {
53 //
54 // CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
55 //
56 InitializeComponent();
57 base.OnInit(e);
58 }
59
60 /**//// <summary>
61 /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
62 /// 此方法的內容。
63 /// </summary>
64 private void InitializeComponent()
65 {
66 this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
67 this.Load += new System.EventHandler(this.Page_Load);
68
69 }
70 #endregion
71
72 private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
73 {
74 if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
75 {
76 e.Item.Attributes.Add("onmouseover", "this.oldcolor=this.style.backgroundColor;this.style.backgroundColor='#C8F7FF';");
77 e.Item.Attributes.Add("onmousemove", "Show('"+dt.Rows[e.Item.ItemIndex]["country"].ToString()+"','"
78 +dt.Rows[e.Item.ItemIndex]["City"].ToString()+"','"
79 +dt.Rows[e.Item.ItemIndex]["Address"].ToString()+"','"
80 +dt.Rows[e.Item.ItemIndex]["PostalCode"].ToString()+"','"
81 +dt.Rows[e.Item.ItemIndex]["Phone"].ToString()+"','"
82 +dt.Rows[e.Item.ItemIndex]["Fax"].ToString()+"');");
83 e.Item.Attributes.Add("onmouseout",
84 "this.style.backgroundColor=this.oldcolor;Hide();");
85 }
86 }
87 }
88}
歡迎關注本站公眾號,獲取更多信息