如何讓Gridview在沒有數(shù)據(jù)的時候顯示表頭_第1頁
如何讓Gridview在沒有數(shù)據(jù)的時候顯示表頭_第2頁
如何讓Gridview在沒有數(shù)據(jù)的時候顯示表頭_第3頁
如何讓Gridview在沒有數(shù)據(jù)的時候顯示表頭_第4頁
如何讓Gridview在沒有數(shù)據(jù)的時候顯示表頭_第5頁
免費(fèi)預(yù)覽已結(jié)束,剩余2頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、如何讓Gridview在沒有數(shù)據(jù)的時候顯示表頭1 前言當(dāng)對GridView 控件進(jìn)行數(shù)據(jù)綁定時,如果綁定的記錄為空,網(wǎng)頁上就不顯示 GridView 造成頁面部分空白,頁面布局結(jié)構(gòu)也受影響。下面討論的方法可以讓GridView 在沒有數(shù)據(jù)記錄的時候顯示表的字段結(jié)構(gòu)和顯示提示信息。2 數(shù)據(jù)為了讓GridView 顯示數(shù)據(jù),在數(shù)據(jù)庫中建立表temple ,其字段如下:temple 表示廟宇,它的字段有:temple_id inttemple_ namevarchar(50)locati on varchar(50)build_date datetimetemple 的數(shù)據(jù)為:temple idte

2、mple namelocati onbuild time1少林寺河南省登封市嵩山1900-2-2 0:00:002大杰寺五龍山1933-2-3 3:03:033法源寺宣武門外教子胡冋南端東側(cè)1941-2-3 5:04:034廣濟(jì)寺阜成門內(nèi)大街東口1950-3-3 3:03:035碧云寺香山東麓1963-3-3 3:03:033 .頁面建立一個asp. net網(wǎng)站工程,在頁面中添加GridView和幾個按鈕,代碼如下所示:<%PageLanguage="C#" AutoEventWireup ="true" CodeFile ="Defau

3、lt.aspx.cs" Inherits ="_Default" %><! DOCTYRfimlPUBLIC"-/W3C/DTD XHTML 1.0Transitional/EN""/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns ="/1999/xhtml"><headrunat ="server"><title >

4、;GridView綁定記錄為空顯示表頭測試 </title ></head><body>< form id ="form1" runat ="server">< div style ="font-size:13px;">< asp : GridView ID ="GridViewEmptyDataTest" runat ="server" AutoGenerateColumns ="False" EmptyDa

5、taText ="Data IsEmpty" BackColor="White"BorderColor="LightGray" BorderStyle="Double" BorderWidth ="3px"CellPadding ="4"GridLines="Horizontal"Width ="500px">< Columns >< asp : BoundFieldDataField="temple_

6、id"HeaderText ="temple_id"Visible ="False"></ asp : BoundField><asp: BoundField DataField ="temple_name" HeaderText ="名稱 ">< ItemStyle BorderColor="LightGray"BorderStyle="Double"BorderWidth="1px"Width ="

7、;100px"/></ asp : BoundField ><asp: BoundField DataField="location" HeaderText ="地址 ">< ItemStyle BorderColor="LightGray"BorderStyle="Double"BorderWidth="1px"Width ="300px"/></ asp : BoundField ><asp: Boun

8、dField DataField="build_date"HeaderText =" 建設(shè)時間 ">< ItemStyle BorderColor="LightGray"BorderStyle="Double"BorderWidth="1px"Width ="100px"/></ asp : BoundField ></ Columns >< FooterStyle BackColor ="White" Fo

9、reColor ="#333333"/>< RowStyle BackColor ="White" ForeColor ="#333333"/>< SelectedRowStyle BackColor ="#339966"Font-Bold ="True" ForeColor ="White"/>< PagerStyle BackColor ="#336666" ForeColor ="White"

10、HorizontalAlign ="Center"/>< HeaderStyle BackColor ="CornflowerBlue"Font-Bold ="True" ForeColor ="White"/></ asp : GridView >< br/><asp: Button ID="ButtonHasDataBind" runat ="server" Text =" 有數(shù)據(jù)綁定" Width =&

11、quot;109px" OnClick ="ButtonHasDataBind_Click"/><asp: Button ID="ButtonQueryEmptyBind" runat ="server" Text =" 查詢結(jié)果為空綁定" Width ="142px" OnClick ="ButtonQueryEmptyBind_Click"/><asp: Button ID="ButtonConstructTableBind&q

12、uot; runat ="server" Text =" 構(gòu)造空的 DataTable 綁定" Width ="164px" OnClick ="ButtonConstructTableBind_Click"/><asp: Button ID="ButtonNormalBind" runat ="server" Text =" 普通空數(shù)據(jù)綁定" Width ="127px" OnClick ="ButtonNorm

13、alBind_Click"/></div ></ form ></ bodyv/html >GridView 要綁定的字段和 temple 的字段一樣,在這里我們利用GridView 原有的功能,設(shè)定當(dāng)數(shù)據(jù)為空是顯示"Data Is Empty ”,如果沒有設(shè)定 EmptyDataText 屬性,當(dāng)綁定 的記錄為空時,GridView 將不在頁面顯示。4 數(shù)據(jù)顯示編寫 ButtonNormalBind4 . 1普通空記錄顯示的事件函數(shù)ButtonNormalBind_Click,添加如下代碼,來測試沒有經(jīng)過處理的 GridView顯

14、示情況:protectedvoid ButtonNormalBind_Click( object sender, EventArgs e)DataTable dt = new DataTable ();dt.Colum ns.Add(dt.Colum ns.Add(dt.Colum ns.Add(dt.Colum ns.Add("temple_id"); "temple_ name");"locatio n"); "build_date");this .GridViewEmptyDataTest.DataSourc

15、e = dt;this .GridViewEmptyDataTest.DataBind();執(zhí)行這些代碼后,GridView顯示結(jié)果如下圖所一Data Is Empty示:可以看到這樣簡單的提示看不岀GridView的結(jié)構(gòu)來,在大多數(shù)的實際應(yīng)用中我們希望看到GridView到底有哪些字段。4 . 2增加空行來顯示GridView 的結(jié)構(gòu)我們知道只要 GridView 綁定的DataTable 有一行記錄,GridView 就會顯示表頭,所以當(dāng)DataTable為空時我們增加一個空行從而顯示表頭。我們把代碼改成如下所示:DataTable dt = newDataTable ();dt.Colu

16、m ns.Add("temple_id");dt.Colu mn s.Add("temple_ name");dt.Colu mn s.Add("locatio n");dt.Colum ns.Add("build_date");if (dt.Rows.Cou nt = 0)dt.Rows.Add(dt.NewRow();this .GridViewEmptyDataTest.DataSource = dt;this .GridViewEmptyDataTest.DataBind();在每次綁定前判斷,如果為空就增

17、加一空行,這樣綁定的結(jié)果如下圖所示:1名稱地址建設(shè)時1可I可以看得表頭已經(jīng)可以顯示了,但是顯示的空行沒有任何數(shù)據(jù)也讓人費(fèi)解,可不可以增加一下提示信息呢?4 . 3增加空記錄提示我們在數(shù)據(jù)綁定后增加一些代碼對GridView進(jìn)行一下處理,讓顯示結(jié)果更友好。在this .GridViewEmptyDataTest.DataBind();后面增加代碼如下所示:int columnCount = dt.Columns.Count;GridViewEmptyDataTest.RowsO.Cells.Clear();GridViewEmptyDataTest.RowsO.Cells.Add(new Tab

18、leCell ();GridViewEmptyDataTest.RowsO.CellsO.Colu mn Spa n = colum nCou nt;GridViewEmptyDataTest.Rows0.Cells0.Text ="沒有記錄”;GridViewEmptyDataTest.Rows0.Cells0.Style.Add("text-align" , "center");改良后的顯示結(jié)果如下圖所示:名琢地址建設(shè)時閭沒有記錄看來顯示結(jié)果已經(jīng)達(dá)到了我們的要求,但是當(dāng)頁面上有其他按鈕操作導(dǎo)致頁面PostBack時,頁面再次顯示確沒有了提示

19、信息變成如下圖所示的樣子:名稱地址建設(shè)時間這并不是我們想要的4 . 4防止PostBack時頁面顯示變化為了防止顯示改變我們在Page_Load事件里添加如下代碼,從而重新綁定GridViewif (IsPostBack)/ 如果數(shù)據(jù)為空則重新構(gòu)造 Gridviewif (GridViewEmptyDataTest.Rows.Count = 1 &&GridViewEmptyDataTest.Rows0.Cells0.Text = "沒有記錄 ")int columnCount = GridViewEmptyDataTest.Columns.Count;Gr

20、idViewEmptyDataTest.Rows0.Cells.Clear();GridViewEmptyDataTest.Rows0.Cells.Add( new TableCell ();GridViewEmptyDataTest.Rows0.Cells0.ColumnSpan =columnCount;GridViewEmptyDataTest.Rows0.Cells0.Text =" 沒有記錄 "GridViewEmptyDataTest.Rows0.Cells0.Style.Add( "text-align" ,"center&quo

21、t; );這下我們的控件終于可以按我們的要求顯示了, 但是為了代碼的重用, 當(dāng)一個項目里有多個 GridView 時,避免充分些相同的代碼, 我們需要把代碼封裝成類, 從而讓所有的 GridView 數(shù) 據(jù)綁定時都可以輕易地實現(xiàn)我們的要求。4 5 封裝類的封裝代碼如下所示:using System.Data ;using System.Web.UI.WebControls ;/<summary>/ Gridview 綁定的數(shù)據(jù)記錄為空時顯示 Gridview 的表頭,并顯示沒有記錄的提示/</summary>publicclass GridviewControl/ 當(dāng)

22、Gridview 數(shù)據(jù)為空時顯示的信息privatestaticstring EmptyText = " 沒有記錄 "public GridviewControl()/<summary>/ 防止 PostBack 后 Gridview 不能顯示/</summary>/<param name="gridview"></param>publicstaticvoid ResetGridView( GridView gridview)/ 如果數(shù)據(jù)為空則重新構(gòu)造 Gridviewif (gridview.Rows.C

23、ount = 1 && gridview.Rows0.Cells0.Text = EmptyText)int columnCount = gridview.Columns.Count;gridview.Rows0.Cells.Clear();gridview.Rows0.Cells.Add( new TableCell (); gridview.Rows0.Cells0.ColumnSpan = columnCount; gridview.Rows0.Cells0.Text = EmptyText; gridview.Rows0.Cells0.Style.Add( "

24、text-align" , "center" );/<summary>/ 綁定數(shù)據(jù)到 GridView ,當(dāng)表格數(shù)據(jù)為空時顯示表頭/</summary>/<param name="gridview"></param>/<param name="table"></param>publicstaticvoid GridViewDataBind( GridView gridview, DataTable table)/ 記錄為空重新構(gòu)造 Gridviewif

25、(table.Rows.Count = 0)table = table.Clone();table.Rows.Add(table.NewRow();gridview.DataSource = table;gridview.DataBind();int columnCount = table.Columns.Count;gridview.Rows0.Cells.Clear();gridview.Rows0.Cells.Add( new TableCell (); gridview.Rows0.Cells0.ColumnSpan = columnCount; gridview.Rows0.Cells0.Text = EmptyText; gridview.Rows0.Cells0.Style.Add( "text-align&quo

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論