日期:2011-01-15  浏览次数:20556 次

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