简单实用的DATAGRID组件
近期由于项目中多处需要用到可固定表头,表体可滚动的DATAGRID。
在网上查阅了一些资料大都是和没封装好的DATAGRID,在实际项目中应用比较繁琐。
在参考:
http://www.datawebcontrols.com/faqs/CustomizingAppearance/ScrollableDataGridWithFixedHeader.shtml
的基础上封装了一个无分页的可固定表头,表体可移动的DATAGRID组件,希望可以为需要此简单功能的同仁提供一点帮助。



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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

功能比较简单,基本功能在项目中已测试。
希望有发现BUG的给予告知,谢谢。
分类:
asp.net
· dotnet 9 通过 AppHostRelativeDotNet 指定自定义的运行时路径
· 如何统计不同电话号码的个数?—位图法
· C#高性能开发之类型系统:从 C# 7.0 到 C# 14 的类型系统演进全景
· 从零实现富文本编辑器#3-基于Delta的线性数据结构模型
· 记一次 .NET某旅行社酒店管理系统 卡死分析
· dotnet 9 通过 AppHostRelativeDotNet 指定自定义的运行时路径
· 一次小而美的重构:使用 C# 在 Avalonia 中生成真正好看的词云
· AI Agent离我们有多远?认知革命的开始(上篇)
· 记一次SQL隐式转换导致精度丢失问题的排查 → 不规范就踩坑
· 物流规划、仓储自动化和算法调度的问题分析