1 //使用方法:
2 //在page_load事件中加入下列样式代码,其中grdPay为GridView. 必须设置grdPay的EmptyDataText属性不为空.
3 if (this.IsPostBack == false)
4 {
5 grdPay.DataBind();
6 }
7 UGridView grd = new UGridView(grdPay);
8
9 //源程序
10 using System;
11 using System.Data;
12 using System.Configuration;
13 using System.Web;
14 using System.Web.Security;
15 using System.Web.UI;
16 using System.Web.UI.WebControls;
17 using System.Web.UI.WebControls.WebParts;
18 using System.Web.UI.HtmlControls;
19
20 public class UGridView
21 {
22 public UGridView(GridView grd)
23 {
24 if (grd.EmptyDataText == "")
25 {
26 grd.EmptyDataText = "没有符合条件的数据";
27 }
28 grd.PreRender += new EventHandler(grd_PreRender);
29 }
30
31 void grd_PreRender(object sender, EventArgs e)
32 {
33 DrawHeader(sender);
34 }
35 private void Grd_RowDataBound(object sender, GridViewRowEventArgs e)
36 {
37 if (e.Row.RowIndex == -1)
38 {
39 DrawHeader(sender);
40 }
41 }
42 private void DrawHeader(object sender)
43 {
44 GridView grd = (GridView)sender;
45 if (grd.Rows.Count > 0) return; //有数据,不要处理
46 if (grd.DataSource != null)
47 {
48 if (((DataTable)grd.DataSource).Rows.Count > 0)
49 {
50 return;
51 }
52 }
53 GridViewRow row = new GridViewRow(-1, -1,DataControlRowType.EmptyDataRow,DataControlRowState.Normal);
54 foreach (DataControlField field in grd.Columns)
55 {
56 TableCell cell = new TableCell();
57 cell.Text = field.HeaderText;
58 cell.Width = field.HeaderStyle.Width;
59 cell.Height = field.HeaderStyle.Height;
60 cell.ForeColor = field.HeaderStyle.ForeColor;
61 cell.Font.Size = field.HeaderStyle.Font.Size;
62 cell.Font.Bold = field.HeaderStyle.Font.Bold;
63 cell.Font.Name = field.HeaderStyle.Font.Name;
64 cell.Font.Strikeout = field.HeaderStyle.Font.Strikeout;
65 cell.Font.Underline = field.HeaderStyle.Font.Underline;
66 cell.BackColor = field.HeaderStyle.BackColor;
67 cell.VerticalAlign = field.HeaderStyle.VerticalAlign;
68 cell.HorizontalAlign = field.HeaderStyle.HorizontalAlign;
69 cell.CssClass = field.HeaderStyle.CssClass;
70 cell.BorderColor = field.HeaderStyle.BorderColor;
71 cell.BorderStyle = field.HeaderStyle.BorderStyle;
72 cell.BorderWidth = field.HeaderStyle.BorderWidth;
73 row.Cells.Add(cell);
74 }
75
76 TableItemStyle headStyle = grd.HeaderStyle;
77 TableItemStyle emptyStyle = grd.EmptyDataRowStyle;
78 emptyStyle.Width = headStyle.Width;
79 emptyStyle.Height = headStyle.Height;
80 emptyStyle.ForeColor = headStyle.ForeColor;
81 emptyStyle.Font.Size = headStyle.Font.Size;
82 emptyStyle.Font.Bold = headStyle.Font.Bold;
83 emptyStyle.Font.Name = headStyle.Font.Name;
84 emptyStyle.Font.Strikeout = headStyle.Font.Strikeout;
85 emptyStyle.Font.Underline = headStyle.Font.Underline;
86 emptyStyle.BackColor = headStyle.BackColor;
87 emptyStyle.VerticalAlign = headStyle.VerticalAlign;
88 emptyStyle.HorizontalAlign = headStyle.HorizontalAlign;
89 emptyStyle.CssClass = headStyle.CssClass;
90 emptyStyle.BorderColor = headStyle.BorderColor;
91 emptyStyle.BorderStyle = headStyle.BorderStyle;
92 emptyStyle.BorderWidth = headStyle.BorderWidth;
93 if (grd.Controls.Count == 0)
94 {
95 grd.Page.Response.Write("<script language='javascript'>alert('必须在初始化表格类之前执行DataBind方法并设置EmptyDataText属性不为空!');</script>");
96 }
97 else
98 {
99 grd.Controls[0].Controls.Clear(); //删除没数据时的提示
100 grd.Controls[0].Controls.AddAt(0, row);
101 }
102
103 }
104
105
2 //在page_load事件中加入下列样式代码,其中grdPay为GridView. 必须设置grdPay的EmptyDataText属性不为空.
3 if (this.IsPostBack == false)
4 {
5 grdPay.DataBind();
6 }
7 UGridView grd = new UGridView(grdPay);
8
9 //源程序
10 using System;
11 using System.Data;
12 using System.Configuration;
13 using System.Web;
14 using System.Web.Security;
15 using System.Web.UI;
16 using System.Web.UI.WebControls;
17 using System.Web.UI.WebControls.WebParts;
18 using System.Web.UI.HtmlControls;
19
20 public class UGridView
21 {
22 public UGridView(GridView grd)
23 {
24 if (grd.EmptyDataText == "")
25 {
26 grd.EmptyDataText = "没有符合条件的数据";
27 }
28 grd.PreRender += new EventHandler(grd_PreRender);
29 }
30
31 void grd_PreRender(object sender, EventArgs e)
32 {
33 DrawHeader(sender);
34 }
35 private void Grd_RowDataBound(object sender, GridViewRowEventArgs e)
36 {
37 if (e.Row.RowIndex == -1)
38 {
39 DrawHeader(sender);
40 }
41 }
42 private void DrawHeader(object sender)
43 {
44 GridView grd = (GridView)sender;
45 if (grd.Rows.Count > 0) return; //有数据,不要处理
46 if (grd.DataSource != null)
47 {
48 if (((DataTable)grd.DataSource).Rows.Count > 0)
49 {
50 return;
51 }
52 }
53 GridViewRow row = new GridViewRow(-1, -1,DataControlRowType.EmptyDataRow,DataControlRowState.Normal);
54 foreach (DataControlField field in grd.Columns)
55 {
56 TableCell cell = new TableCell();
57 cell.Text = field.HeaderText;
58 cell.Width = field.HeaderStyle.Width;
59 cell.Height = field.HeaderStyle.Height;
60 cell.ForeColor = field.HeaderStyle.ForeColor;
61 cell.Font.Size = field.HeaderStyle.Font.Size;
62 cell.Font.Bold = field.HeaderStyle.Font.Bold;
63 cell.Font.Name = field.HeaderStyle.Font.Name;
64 cell.Font.Strikeout = field.HeaderStyle.Font.Strikeout;
65 cell.Font.Underline = field.HeaderStyle.Font.Underline;
66 cell.BackColor = field.HeaderStyle.BackColor;
67 cell.VerticalAlign = field.HeaderStyle.VerticalAlign;
68 cell.HorizontalAlign = field.HeaderStyle.HorizontalAlign;
69 cell.CssClass = field.HeaderStyle.CssClass;
70 cell.BorderColor = field.HeaderStyle.BorderColor;
71 cell.BorderStyle = field.HeaderStyle.BorderStyle;
72 cell.BorderWidth = field.HeaderStyle.BorderWidth;
73 row.Cells.Add(cell);
74 }
75
76 TableItemStyle headStyle = grd.HeaderStyle;
77 TableItemStyle emptyStyle = grd.EmptyDataRowStyle;
78 emptyStyle.Width = headStyle.Width;
79 emptyStyle.Height = headStyle.Height;
80 emptyStyle.ForeColor = headStyle.ForeColor;
81 emptyStyle.Font.Size = headStyle.Font.Size;
82 emptyStyle.Font.Bold = headStyle.Font.Bold;
83 emptyStyle.Font.Name = headStyle.Font.Name;
84 emptyStyle.Font.Strikeout = headStyle.Font.Strikeout;
85 emptyStyle.Font.Underline = headStyle.Font.Underline;
86 emptyStyle.BackColor = headStyle.BackColor;
87 emptyStyle.VerticalAlign = headStyle.VerticalAlign;
88 emptyStyle.HorizontalAlign = headStyle.HorizontalAlign;
89 emptyStyle.CssClass = headStyle.CssClass;
90 emptyStyle.BorderColor = headStyle.BorderColor;
91 emptyStyle.BorderStyle = headStyle.BorderStyle;
92 emptyStyle.BorderWidth = headStyle.BorderWidth;
93 if (grd.Controls.Count == 0)
94 {
95 grd.Page.Response.Write("<script language='javascript'>alert('必须在初始化表格类之前执行DataBind方法并设置EmptyDataText属性不为空!');</script>");
96 }
97 else
98 {
99 grd.Controls[0].Controls.Clear(); //删除没数据时的提示
100 grd.Controls[0].Controls.AddAt(0, row);
101 }
102
103 }
104
105