在开发的时候,很多时候都要用到GridView,但是,当GridView没有数据时,GridView就显示不出来。很难看,正好最近做项目用到此方法,就把代码贴出来,供大家学习及自己以后备用。方法很简单,就是在GridView套用一个Table。
特别要注意此处<td colspan="3"> 没有相关数据 </td> colspan="3"是批GridView有多少列。
代码
1
<
asp:GridView ID
=
"
grvStudentInfo
"
runat
=
"
server
"
CssClass
=
"
listgrid
"
AutoGenerateColumns
=
"
False
"
2 AllowSorting = " True " DataKeyNames = " ID "
3 OnRowDeleting = " grvStudentInfo_RowDeleting "
4 onrowdatabound = " grvStudentInfo_RowDataBound " >
5 < EmptyDataTemplate >
6 < table style = " text-align: center " >
7 < tr >
8 < th style = " width: 100px; " >
9 学生姓名
10 </ th >
11 < th >
12 学号
13 </ th >
14 < th >
15 删除
16 </ th >
17 </ tr >
18 < tr align = " center " >
19 < td colspan = " 3 " >
20 没有相关数据
21 </ td >
22 </ tr >
23 </ table >
24 </ EmptyDataTemplate >
25 < Columns >
26 < asp:BoundField DataField = " StudentName " HeaderText = " 学生姓名 " />
27 < asp:BoundField DataField = " DetermineStuNo " HeaderText = " 学号 " />
28 < asp:CommandField HeaderText = " 删除 " ShowDeleteButton = " True " />
29 </ Columns >
30 </ asp:GridView >
2 AllowSorting = " True " DataKeyNames = " ID "
3 OnRowDeleting = " grvStudentInfo_RowDeleting "
4 onrowdatabound = " grvStudentInfo_RowDataBound " >
5 < EmptyDataTemplate >
6 < table style = " text-align: center " >
7 < tr >
8 < th style = " width: 100px; " >
9 学生姓名
10 </ th >
11 < th >
12 学号
13 </ th >
14 < th >
15 删除
16 </ th >
17 </ tr >
18 < tr align = " center " >
19 < td colspan = " 3 " >
20 没有相关数据
21 </ td >
22 </ tr >
23 </ table >
24 </ EmptyDataTemplate >
25 < Columns >
26 < asp:BoundField DataField = " StudentName " HeaderText = " 学生姓名 " />
27 < asp:BoundField DataField = " DetermineStuNo " HeaderText = " 学号 " />
28 < asp:CommandField HeaderText = " 删除 " ShowDeleteButton = " True " />
29 </ Columns >
30 </ asp:GridView >