这篇文章中,就可以在dataview中显示记录了,但是如果你的字段名是英文或者你想设置一下dataview的样式,使其更美观,就不能使用这么简单的代码显示了。
首先,如果要把标题显示为汉字,那么需要设置dataview的autogeneratecolumns="false",不使用默认字段名,然后再设置asp:boundfield的headertext属性,来改变列标题;另外可以设置headerstyle中的backcolor、font-size、horizontalalign等属性,使其变得更加美观。下面给一个我自己做的例子:
<%@ page language="vb" debug ="true" %>
<%@ import namespace="system.data.oledb" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<script runat="server">
dim connstr as string
dim sql as string
dim mycommand as oledbcommand
dim myread as oledbdatareader
dim conn as oledbconnection
sub page_load(byval sender as object, byval e as eventargs)
connstr = "provider=microsoft.jet.oledb.4.0;data source=" & server.mappath("rizhi.mdb") & ";user id=;password=;"
conn = new oledbconnection(connstr)
conn.open()
sql = "select * from rizhi"
mycommand = new oledbcommand(sql, conn)
myread = mycommand.executereader()
gridview1.datasource = myread
gridview1.databind()
'dim i as integer
'do while myread.read()
' i = i + 1
'loop
'response.write(i)
end sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<center>
<asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" width ="60%" font-size="12px" >
<columns>
<asp:boundfield datafield="id" headertext="序号" >
<itemstyle font-size="12px" horizontalalign="left" />
<headerstyle backcolor="burlywood" font-size="12px" horizontalalign="center" />
</asp:boundfield>
<asp:boundfield datafield="rq" headertext="日期" >
<itemstyle font-size="12px" horizontalalign="left" />
<headerstyle backcolor="burlywood" font-size="12px" horizontalalign="center" />
</asp:boundfield>
<asp:boundfield datafield="n_je" headertext="金额" >
<itemstyle font-size="12px" horizontalalign="right" />
<headerstyle backcolor="burlywood" font-size="12px" horizontalalign="center" />
</asp:boundfield>
<asp:boundfield datafield="title" headertext="标题" >
<itemstyle font-size="12px" horizontalalign="left" />
<headerstyle backcolor="burlywood" font-size="12px" horizontalalign="center" />
</asp:boundfield>
<asp:boundfield datafield="content" headertext="内容" >
<itemstyle font-size="12px" horizontalalign="left" />
<headerstyle backcolor="burlywood" font-size="12px" horizontalalign="center" />
</asp:boundfield>
<asp:hyperlinkfield datatextfield="id" datanavigateurlfields ="id" datanavigateurlformatstring=" rizhi_edit.aspx?id={0} " headertext ="操作" datatextformatstring ="修改" >
<itemstyle font-size="12px" horizontalalign="center" />
<headerstyle backcolor ="burlywood" font-size="12px" horizontalalign="center" />
</asp:hyperlinkfield>
</columns>
</asp:gridview>
</center>
</div>
</form>
</body>
</html>
值得注意的是,这里不光显示了数据,而且还有一列是对数据的“操作”,当用户点击该列的时候,转到修改页面,并传递一个参数id,这里传递参数的方法是 datanavigateurlformatstring="<% rizhi_edit.aspx?id={0} %>" ,其中0表示第一个字段,1表示第二个字段,依此类推。
新闻热点
疑难解答
图片精选