ASP.NET查詢ACCESS資料庫的內容並在DATAVIEW中顯示出來

來源:互聯網
上載者:User
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" "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表示第二個欄位,依此類推。



相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。