ASP.NET查询ACCESS数据库的内容并在DATAVIEW中显示出来

80酷酷网    80kuku.com

  access|asp.net|数据|数据库|显示

这篇文章中,就可以在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" "">

<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="" >
<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表示第二个字段,依此类推。

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: