简单实用的DATAGRID组件
近期由于项目中多处需要用到可固定表头,表体可滚动的DATAGRID。
在网上查阅了一些资料大都是和没封装好的DATAGRID,在实际项目中应用比较繁琐。
在参考:
http://www.datawebcontrols.com/faqs/CustomizingAppearance/ScrollableDataGridWithFixedHeader.shtml
的基础上封装了一个无分页的可固定表头,表体可移动的DATAGRID组件,希望可以为需要此简单功能的同仁提供一点帮助。
<style type="text/css">
.DataGridFixedHeader { FONT-WEIGHT: bold; COLOR: white; POSITION: relative; ; TOP: expression(this.offsetParent.scrollTop - 2); BACKGROUND-COLOR: gray }
</style>
1
using System;
2
using System.Web.UI;
3
using System.Web.UI.WebControls;
4
using System.ComponentModel;
5
using System.Diagnostics;
6
using System.IO;
7
using System.Text;
8
using System.Drawing;
9
10
11
namespace CustomControls
12
{
13
/// <summary>
14
/// CustomDataGrid 的摘要说明。
15
/// Function:Fixed Header and Scrollable DataGrid control
16
/// </summary>
17
public class CustomDataGrid:System.Web.UI.WebControls.DataGrid
18
{
19
public CustomDataGrid():base()
20
{
21
//
22
// TODO: 在此处添加构造函数逻辑
23
//
24
this.HeaderStyle.BackColor=System.Drawing.ColorTranslator.FromHtml("#D8C8A8");
25
this.Style.Add("font-size","10pt");
26
this.BorderWidth=0;
27
this.CellSpacing=1;
28
this.CellPadding=3;
29
this.AlternatingItemStyle.BackColor=System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
30
this.ItemStyle.BackColor=System.Drawing.ColorTranslator.FromHtml("#DDDDDD");
31
32
this.AllowPaging=true;
33
this.AllowSorting=true;
34
this.AllowCustomPaging=false;
35
this.AutoGenerateColumns=false;
36
this.PagerStyle.Visible=false;
37
HeaderStyle.Wrap = false;
38
ItemStyle.Wrap = false;
39
this.FooterStyle.Wrap = false;
40
this.HeaderStyle.Height=18;
41
ItemStyle.Height = 15;
42
//header style
43
this.HeaderStyle.CssClass = "ms-formlabel DataGridFixedHeader";
44
}
45
protected override void Render(HtmlTextWriter output)
46
{
47
//Actual length
48
string actwidth = (this.Width.Type==UnitType.Pixel)?(Width.Value+20)+"px":(Width.Value-5)+"%";
49
output.WriteBeginTag("div");
50
output.WriteAttribute("id",ID + "_div");
51
output.WriteAttribute("style",
52
"HEIGHT: " + Height + ";" +
53
"WIDTH: " + actwidth+";"+
54
"OVERFLOW: auto;"+
55
"TABLE-LAYOUT:fixed;"
56
);
57
output.Write(HtmlTextWriter.TagRightChar);
58
base.Render(output);
59
output.WriteEndTag("div");
60
//add script for adjust Height
61
string adjustHeightScript = @"<script>try{"+this.ID+".style.height=\"\"}catch(e){}</script>";
62
Page.RegisterStartupScript("dummyKey" + this.ID, adjustHeightScript);
63
}
64
65
protected override void OnInit(EventArgs e)
66
{
67
//default height and wedth
68
if (0 == Width.Value) Width = new Unit("300px");
69
if (0 == Height.Value) Height = new Unit("150px");
70
base.OnInit (e);
71
}
72
73
protected override void OnItemCreated(DataGridItemEventArgs e)
74
{
75
for(int i=0;i<e.Item.Cells.Count;i++)
76
{
77
//
78
e.Item.Cells[i].Attributes.Add("nowrap","nowrap");
79
}
80
if(e.Item.ItemType == ListItemType.AlternatingItem ||e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.SelectedItem)
81
{
82
83
e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor='#fff7ce';";
84
if(e.Item.ItemType==ListItemType.Item)
85
e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#dedfde';";
86
else
87
e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#ffffff';";
88
89
}
90
}
91
92
/// <summary>
93
/// The deletion confirms prompts
94
/// </summary>
95
/// <param name="e"></param>
96
protected override void OnItemDataBound(DataGridItemEventArgs e)
97
{
98
foreach(TableCell tc in e.Item.Cells)
99
{
100
if(tc.HasControls())
101
{
102
if(tc.Controls[0].GetType().Name=="DataGridLinkButton")
103
{
104
LinkButton lb=(LinkButton)tc.Controls[0];
105
if(lb.CommandName=="Delete")
106
lb.Attributes["onclick"]="javascript:if(!confirm('are you sue?'))return false;";
107
}
108
}
109
}
110
111
base.OnItemDataBound(e);
112
}
113
}
114
115
}
116
117
118
using System;2
using System.Web.UI;3
using System.Web.UI.WebControls;4
using System.ComponentModel;5
using System.Diagnostics;6
using System.IO;7
using System.Text;8
using System.Drawing;9

10

11
namespace CustomControls12
{13
/// <summary>14
/// CustomDataGrid 的摘要说明。15
/// Function:Fixed Header and Scrollable DataGrid control 16
/// </summary>17
public class CustomDataGrid:System.Web.UI.WebControls.DataGrid18
{19
public CustomDataGrid():base()20
{21
//22
// TODO: 在此处添加构造函数逻辑23
//24
this.HeaderStyle.BackColor=System.Drawing.ColorTranslator.FromHtml("#D8C8A8");25
this.Style.Add("font-size","10pt");26
this.BorderWidth=0;27
this.CellSpacing=1;28
this.CellPadding=3;29
this.AlternatingItemStyle.BackColor=System.Drawing.ColorTranslator.FromHtml("#FFFFFF");30
this.ItemStyle.BackColor=System.Drawing.ColorTranslator.FromHtml("#DDDDDD");31

32
this.AllowPaging=true;33
this.AllowSorting=true;34
this.AllowCustomPaging=false;35
this.AutoGenerateColumns=false;36
this.PagerStyle.Visible=false;37
HeaderStyle.Wrap = false;38
ItemStyle.Wrap = false;39
this.FooterStyle.Wrap = false;40
this.HeaderStyle.Height=18;41
ItemStyle.Height = 15;42
//header style43
this.HeaderStyle.CssClass = "ms-formlabel DataGridFixedHeader";44
}45
protected override void Render(HtmlTextWriter output)46
{47
//Actual length48
string actwidth = (this.Width.Type==UnitType.Pixel)?(Width.Value+20)+"px":(Width.Value-5)+"%";49
output.WriteBeginTag("div");50
output.WriteAttribute("id",ID + "_div");51
output.WriteAttribute("style",52
"HEIGHT: " + Height + ";" +53
"WIDTH: " + actwidth+";"+54
"OVERFLOW: auto;"+55
"TABLE-LAYOUT:fixed;"56
);57
output.Write(HtmlTextWriter.TagRightChar);58
base.Render(output);59
output.WriteEndTag("div");60
//add script for adjust Height61
string adjustHeightScript = @"<script>try{"+this.ID+".style.height=\"\"}catch(e){}</script>";62
Page.RegisterStartupScript("dummyKey" + this.ID, adjustHeightScript);63
} 64

65
protected override void OnInit(EventArgs e)66
{67
//default height and wedth68
if (0 == Width.Value) Width = new Unit("300px");69
if (0 == Height.Value) Height = new Unit("150px");70
base.OnInit (e);71
}72

73
protected override void OnItemCreated(DataGridItemEventArgs e)74
{75
for(int i=0;i<e.Item.Cells.Count;i++)76
{77
//78
e.Item.Cells[i].Attributes.Add("nowrap","nowrap");79
}80
if(e.Item.ItemType == ListItemType.AlternatingItem ||e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.SelectedItem)81
{82

83
e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor='#fff7ce';";84
if(e.Item.ItemType==ListItemType.Item)85
e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#dedfde';";86
else87
e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#ffffff';";88

89
}90
}91

92
/// <summary>93
/// The deletion confirms prompts94
/// </summary>95
/// <param name="e"></param>96
protected override void OnItemDataBound(DataGridItemEventArgs e)97
{98
foreach(TableCell tc in e.Item.Cells)99
{100
if(tc.HasControls())101
{102
if(tc.Controls[0].GetType().Name=="DataGridLinkButton")103
{104
LinkButton lb=(LinkButton)tc.Controls[0];105
if(lb.CommandName=="Delete")106
lb.Attributes["onclick"]="javascript:if(!confirm('are you sue?'))return false;";107
}108
}109
}110
111
base.OnItemDataBound(e);112
}113
}114

115
}116

117

118

功能比较简单,基本功能在项目中已测试。
希望有发现BUG的给予告知,谢谢。
.DataGridFixedHeader
}
浙公网安备 33010602011771号