本文目录一览:
- 1、如何让gridview在没有数据时显示表头
- 2、gridview 表头如何居中
- 3、命名空间不能直接包含字段或方法之类的成员
- 4、DataTable绑定到datagrid后,怎么在程序中设置各列的居中属性
- 5、asp.net中在Gridview中自定义一列操作,应该怎么做?
如何让gridview在没有数据时显示表头
如何让gridview在没有数据时显示表头
1.前言
当对GridView控件进行数据绑定时,如果绑定的记录为空,网页上就不显示GridView,造成页面部分空白,页面布局结构也受影响。下面讨论的方法可以让GridView在没有数据记录的时候显示表的字段结构和显示提示信息。-horizontalalign
2.数据
为了让GridView显示数据,在数据库中建立表temple,其字段如下:
temple表示庙宇,它的字段有:
temple_id int
temple_name varchar(50)
location varchar(50)
build_date datetime
3.页面
建立一个asp.net网站首悄工程,在页面中添加GridView和几个按钮,代码如下所示:
%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head runat="server"
titleGridView绑定记录为空显示表头测试/title
/head
body
form id="form1" runat="server"
div style="font-size:13px;"
asp:GridView ID="GridViewEmptyDataTest" runat="server" AutoGenerateColumns="False" EmptyDataText="Data Is Empty" BackColor="White" BorderColor="LightGray" BorderStyle="Double" BorderWidth="3px"-horizontalalign
CellPadding="4" GridLines="Horizontal" Width="500px"
Columns
asp:BoundField DataField="temple_id" HeaderText="temple_id" Visible="False"者拆渣
/asp:BoundField
asp:BoundField DataField="temple_name" HeaderText="名称"
ItemStyle BorderColor="LightGray" BorderStyle="Double" BorderWidth="1px" Width="100px" /
/asp:BoundField
asp:BoundField DataField="location" HeaderText="地址"
ItemStyle BorderColor="LightGray" BorderStyle="Double" BorderWidth="1px"御闭 Width="300px" /
/asp:BoundField
asp:BoundField DataField="build_date" HeaderText="建设时间"
ItemStyle BorderColor="LightGray" BorderStyle="Double" BorderWidth="1px" Width="100px" /
/asp:BoundField
/Columns
FooterStyle BackColor="White" ForeColor="#333333" /
RowStyle BackColor="White" ForeColor="#333333" /
SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" /
PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" /
HeaderStyle BackColor="CornflowerBlue" Font-Bold="True" ForeColor="White" /
/asp:GridView
br /
asp:Button ID="ButtonHasDataBind" runat="server" Text="有数据绑定" Width="109px" OnClick="ButtonHasDataBind_Click" /-horizontalalign
asp:Button ID="ButtonQueryEmptyBind" runat="server" Text="查询结果为空绑定" Width="142px" OnClick="ButtonQueryEmptyBind_Click" /-horizontalalign
asp:Button ID="ButtonConstructTableBind" runat="server" Text="构造空的DataTable绑定" Width="164px" OnClick="ButtonConstructTableBind_Click" /-horizontalalign
asp:Button ID="ButtonNormalBind" runat="server" Text="普通空数据绑定" Width="127px" OnClick="ButtonNormalBind_Click" //div-horizontalalign
/form
/body
/html
GridView要绑定的字段和temple的字段一样,在这里我们利用GridView原有的功能,设定当数据为空是显示“Data Is Empty”,如果没有设定EmptyDataText属性,当绑定的记录为空时,GridView将不在页面显示。-horizontalalign
gridview 表头如何居中
vs 2008
Private Sub tableHeadSet()
'/* set the table head */
Dim tcHeader As TableCellCollection = GridView1.HeaderRow.Cells
'第告斗一行表头
tcHeader.Clear()
tcHeader.Add(New TableHeaderCell())
tcHeader(0).BackColor = Drawing.Color.DarkBlue
tcHeader(0).ForeColor = Drawing.Color.White
tcHeader(0).Height = 30
tcHeader(0).Attributes.Add("colspan", "16") '跨Column
'tcHeader(0).Font.Size = 9
'tcHeader(0).Font.Bold = False
tcHeader(0).Text = "采油井参数显示镇源/袜旅磨th/trtr"
'第二行表头
tcHeader.Add(New TableHeaderCell())
tcHeader(1).BackColor = Drawing.Color.DarkMagenta
tcHeader(1).ForeColor = Drawing.Color.White
tcHeader(1).HorizontalAlign = HorizontalAlign.Center
tcHeader(1).Width = 150
tcHeader(1).Text = "油井名称"
tcHeader.Add(New TableHeaderCell())
tcHeader(2).BackColor = Drawing.Color.DarkGreen
tcHeader(2).ForeColor = Drawing.Color.White
tcHeader(2).HorizontalAlign = HorizontalAlign.Center
tcHeader(2).Width = 140
tcHeader(2).Text = "巡井时间"
tcHeader.Add(New TableHeaderCell())
tcHeader(3).BackColor = Drawing.Color.DarkMagenta
tcHeader(3).ForeColor = Drawing.Color.White
tcHeader(3).HorizontalAlign = HorizontalAlign.Center
tcHeader(3).Width = 80
tcHeader(3).Text = "油井状态"
tcHeader.Add(New TableHeaderCell())
tcHeader(4).BackColor = Drawing.Color.DarkGreen
tcHeader(4).HorizontalAlign = HorizontalAlign.Center
tcHeader(4).ForeColor = Drawing.Color.White
tcHeader(4).Width = 70
tcHeader(4).Text = "冲程(m)"
tcHeader.Add(New TableHeaderCell())
tcHeader(5).BackColor = Drawing.Color.DarkMagenta
tcHeader(5).ForeColor = Drawing.Color.White
tcHeader(5).HorizontalAlign = HorizontalAlign.Center
tcHeader(5).Width = 90
tcHeader(5).Text = "冲次(n/M)"
tcHeader.Add(New TableHeaderCell())
tcHeader(6).BackColor = Drawing.Color.DarkGreen
tcHeader(6).ForeColor = Drawing.Color.White
tcHeader(6).HorizontalAlign = HorizontalAlign.Center
tcHeader(6).Width = 80
tcHeader(6).Text = "最大载荷(KN)"
tcHeader.Add(New TableHeaderCell())
tcHeader(7).BackColor = Drawing.Color.DarkMagenta
tcHeader(7).ForeColor = Drawing.Color.White
tcHeader(7).HorizontalAlign = HorizontalAlign.Center
tcHeader(7).Width = 80
tcHeader(7).Text = "最小载荷(KN)"
tcHeader.Add(New TableHeaderCell())
tcHeader(8).BackColor = Drawing.Color.DarkGreen
tcHeader(8).ForeColor = Drawing.Color.White
tcHeader(8).HorizontalAlign = HorizontalAlign.Center
tcHeader(8).Width = 70
tcHeader(8).Text = "A相最大电流(A)"
tcHeader.Add(New TableHeaderCell())
tcHeader(9).BackColor = Drawing.Color.DarkMagenta
tcHeader(9).ForeColor = Drawing.Color.White
tcHeader(9).HorizontalAlign = HorizontalAlign.Center
tcHeader(9).Width = 70
tcHeader(9).Text = "A相最小电流(A)"
tcHeader.Add(New TableHeaderCell())
tcHeader(10).BackColor = Drawing.Color.DarkGreen
tcHeader(10).ForeColor = Drawing.Color.White
tcHeader(10).HorizontalAlign = HorizontalAlign.Center
tcHeader(10).Width = 70
tcHeader(10).Text = "Uab电压(V)"
tcHeader.Add(New TableHeaderCell())
tcHeader(11).BackColor = Drawing.Color.DarkMagenta
tcHeader(11).ForeColor = Drawing.Color.White
tcHeader(11).HorizontalAlign = HorizontalAlign.Center
tcHeader(11).Width = 70
tcHeader(11).Text = "Ubc电压(V)"
tcHeader.Add(New TableHeaderCell())
tcHeader(12).BackColor = Drawing.Color.DarkGreen
tcHeader(12).ForeColor = Drawing.Color.White
tcHeader(12).HorizontalAlign = HorizontalAlign.Center
tcHeader(12).Width = 70
tcHeader(12).Text = "Uca电压(V)"
tcHeader.Add(New TableHeaderCell())
tcHeader(13).BackColor = Drawing.Color.DarkMagenta
tcHeader(13).ForeColor = Drawing.Color.White
tcHeader(13).HorizontalAlign = HorizontalAlign.Center
tcHeader(13).Width = 70
tcHeader(13).Text = "A相电流(A)"
tcHeader.Add(New TableHeaderCell())
tcHeader(14).BackColor = Drawing.Color.DarkGreen
tcHeader(14).ForeColor = Drawing.Color.White
tcHeader(14).HorizontalAlign = HorizontalAlign.Center
tcHeader(14).Width = 70
tcHeader(14).Text = "B相电流(A)"
tcHeader.Add(New TableHeaderCell())
tcHeader(15).BackColor = Drawing.Color.DarkMagenta
tcHeader(15).ForeColor = Drawing.Color.White
tcHeader(15).HorizontalAlign = HorizontalAlign.Center
tcHeader(15).Width = 70
tcHeader(15).Text = "C相电流(A)"
tcHeader.Add(New TableHeaderCell())
tcHeader(16).BackColor = Drawing.Color.DarkGreen
tcHeader(16).ForeColor = Drawing.Color.White
tcHeader(16).HorizontalAlign = HorizontalAlign.Center
tcHeader(16).Width = 80
tcHeader(16).Text = "井口压力(Mpa)/th/trtr"
End Sub
要想使表头字体居中,要注意表头各列列宽,表头总列宽,才可以达到完美。
GridView1.Width = 1400
命名空间不能直接包含字段或方法之类的成员
APP_CODE 写的类 在同一WEB项目的代码手亮中使用庆薯槐不需要誉友额外引用。
namespace N
{
public Class XXX{}
}
在default.cs中只需要 N.XXX x=new N.XXX 即可。
DataTable绑定到datagrid后,怎么在程序中设置各列的居中属性
DataTable绑定到datagrid后,在程序中乱哪设置各列的居中属性方法:
使用下面的语句可以定义列标题的对齐方式
Datagrid1.Columns(0).Alignment = 2
Datagrid1.Refresh
其中困茄将Aligment设置为0为左对齐、1为右对齐、2为居中对齐
也可以用数据内容居汪陪察中
private void DataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
for (int i=0; i e.Item.Cells.Count; i++)
e.Item.Cells[i].HorizontalAlign=HorizontalAlign.Center;
}
asp.net中在Gridview中自定义一列操作,应该怎么做?
思前宽路:先定义慧态亮事件,在linkbutton上绑定事件,也就是一个CRUD的方法,然后在后台的OnRowCommand事件里调用绑定的事件即可。
具体举例如下:
1、前端的代码如下:
asp:GridView ID="gridViewDxjk" CssClass="gridview" runat="server" AllowPaging="True"
DataKeyNames="P_ID" AutoGenerateColumns="False"
RowStyle-HorizontalAlign="Center" BorderWidth="1px" PageSize="17"
onrowdeleting="gridViewDxjk_RowDeleting"
OnRowDataBound="gridViewDxjk_RowDataBound"
onpageindexchanging="gridViewDxjk_PageIndexChanging"
HeaderStyle CssClass="head" /
PagerStyle CssClass="pager" /
RowStyle CssClass="row" /
EditRowStyle CssClass="editrow" /
AlternatingRowStyle CssClass="altrow" /
EmptyDataRowStyle CssClass="empty" /
Columns
asp:HyperLinkField HeaderText="编辑" ControlStyle-Width="50" DataNavigateUrlFields="P_ID" DataNavigateUrlFormatString="smsModify.aspx?id={0}" Text="编辑" -horizontalalign
ControlStyle Width="50px"/ControlStyle/asp:HyperLinkField
asp:CommandField ShowDeleteButton="true" DeleteText="删除" 闭宏
ControlStyle Width="50px"/ControlStyle/asp:CommandField
asp:BoundField DataField="P_ID" HeaderText="id" SortExpression="P_ID" ItemStyle-HorizontalAlign="Center" Visible="False" -horizontalalign
ItemStyle HorizontalAlign="Center"/ItemStyle/asp:BoundField
asp:BoundField DataField="P_NAME" HeaderText="名称" SortExpression="P_NAME" /-horizontalalign
asp:BoundField DataField="P_Type" HeaderText="通知方式" SortExpression="P_Type" ItemStyle-HorizontalAlign="Center" -horizontalalign
ItemStyle HorizontalAlign="Center"/ItemStyle/asp:BoundField
asp:BoundField DataField="P_Fzr" HeaderText="姓名" SortExpression="P_Fzr" ItemStyle-HorizontalAlign="Center" -horizontalalign
ItemStyle HorizontalAlign="Center"/ItemStyle/asp:BoundField
asp:BoundField DataField="P_tel" HeaderText="通知手机" SortExpression="P_tel" ItemStyle-HorizontalAlign="Center" -horizontalalign
ItemStyle HorizontalAlign="Center"/ItemStyle/asp:BoundField
asp:BoundField DataField="P_jg" HeaderText="通知间隔(小时)" SortExpression="P_jg" ItemStyle-HorizontalAlign="Center" -horizontalalign
ItemStyle HorizontalAlign="Center"/ItemStyle/asp:BoundField
asp:BoundField DataField="P_on" HeaderText="是否开启" SortExpression="P_on" ItemStyle-HorizontalAlign="Center" -horizontalalign
ItemStyle HorizontalAlign="Center"/ItemStyle/asp:BoundField
asp:BoundField DataField="P_lasttime" HeaderText="最后发送时间" SortExpression="P_lasttime" ItemStyle-HorizontalAlign="Center" -horizontalalign
ItemStyle HorizontalAlign="Center"/ItemStyle/asp:BoundField
asp:BoundField DataField="P_memo" HeaderText="备注" SortExpression="P_memo" ItemStyle-HorizontalAlign="Center" -horizontalalign
ItemStyle HorizontalAlign="Center"/ItemStyle/asp:BoundField
/Columns
EmptyDataTemplate
没有数据!
/EmptyDataTemplate
PagerTemplate
table width="100%" class="gvPage" style="font-size:12px;"
tr
td style="text-align: right"
第asp:Label ID="lblPageIndex" runat="server" Text='%# ((GridView)Container.Parent.Parent).PageIndex + 1 %'/asp:Label页-horizontalalign
/共asp:Label ID="lblPageCount" runat="server" Text='%# ((GridView)Container.Parent.Parent).PageCount %'/asp:Label页 -horizontalalign
asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page" Visible="%# ((GridView)Container.NamingContainer).PageIndex != 0 %"首页/asp:LinkButton-horizontalalign
asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page" Visible="%# ((GridView)Container.NamingContainer).PageIndex != 0 %"上一页/asp:LinkButton-horizontalalign
asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page" Visible="%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %"下一页/asp:LinkButton-horizontalalign
asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page" Visible="%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %"尾页/asp:LinkButton-horizontalalign
asp:TextBox ID="txtNewPageIndex" runat="server" Text='%# ((GridView)Container.Parent.Parent).PageIndex + 1 %' Width="20px" AutoPostBack="true" /asp:TextBox-horizontalalign
asp:LinkButton ID="btnGoEx" runat="server" CommandArgument="GO" CommandName="Page" Text="GO" OnClick="btnGoEx_Click"/asp:LinkButton-horizontalalign
/td
/tr
/table
/PagerTemplate
/asp:GridView
2、后端的话,由于需要弹出删除前的确认框,所以,这里需要在RowDataBound里面做点逻辑,同时,要想真正的删除,还需要触发RowDeleting事件,具体代码如下:
//报警删除
protected void gridViewDxjk_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string key = gridViewDxjk.DataKeys[e.RowIndex].Value.ToString();
bool flag = bll.Delete(Int32.Parse(key));
if (flag)
NXT_WLService.App_Code.JScript.Alert("删除成功!", this);
else
NXT_WLService.App_Code.JScript.Alert("删除失败!", this);
}
protected void gridViewDxjk_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton btn = (LinkButton)e.Row.Cells[1].Controls[0];
if (btn.Text.Equals("删除"))//删除钮才加提示讯息
btn.OnClientClick = "if (confirm('你确认要删除?')) javascript:__doPostBack('gridViewDxjk','Delete$" + e.Row.RowIndex.ToString() + "'); else return false;";-horizontalalign
}
}