皓翔的学习网站

-------Uml .NET RUP相关技术

 

用DatGrid显示CSV文件

这段代码显示了如何在Asp.NET中读取.CSV文件并使用DataGrid控件显示
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="VB" runat="server">
    
dim x as integer
Sub Page_Load(Source as Object, E as EventArgs)
    GetCSV()
End Sub


Sub GetCSV()
    
Dim strConn As String
    strConn 
= "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    
"Data Source=c:inetpubwwwroot;Extended Properties=Text;"
    Dim Conn As New System.Data.OleDb.OleDbConnection(strConn)
    Conn.Open()
    
    
Dim da As New System.Data.OleDb.OleDbDataAdapter("Select * from lotto.csv", Conn)
    
Dim ds As New DataSet("lotto.csv")

    da.Fill(ds, 
"lotto.csv")
    
Dim dt As DataTable
    dt 
= ds.Tables("lotto.csv")
    MyDataGrid.Datasource
=ds.Tables("lotto.csv").DefaultView
    MyDataGrid.DataBind
End Sub


Sub DataBound (s as Object, e as DatagridItemEventArgs)
Select Case e.Item.ItemType
Case ListItemType.Header
    e.Item.Cells(
0).Text="<b>Month</b>"
    e.Item.Cells(1).Text="<b>Day</b"
    e.Item.Cells(2).Text="<b>Year</b>"
    e.Item.Cells(3).Text="<b>First</b>"
    e.Item.Cells(4).Text="<b>Second</b>"
    e.Item.Cells(5).Text="<b>Third</b>"
    e.Item.Cells(6).Text="<b>Fourth</b>"
    e.Item.Cells(7).Text="<b>Fifth</b>"
    e.Item.Cells(8).Text="<b>Sixth</b>"
    For x=0 to 8
    e.item.cells(x).Font.Size
= FontUnit.Point (14)
    
next
    

Case ListItemType.Item, ListItemType.AlternatingItem

    
For x=0 to 8
    e.item.Cells(x).HorizontalAlign
=HorizontalAlign.Center
    e.item.cells(x).Font.Size
= FontUnit.Point (11)
    
next
End Select
End Sub

</script>

<div align="center">
<form runat="server" method="post">
<b><font Size="5" Color="#0000FF" Face="Arial">Lotto Texas Results</font></b>
<asp:Datagrid runat="server"
    Id="MyDataGrid"
    GridLines="Both"
    cellpadding="2"
    cellspacing="0"
    Headerstyle-BackColor="#8080C0"
    Headerstyle-Font-Name="Arial"
    Headerstyle-Font-Size="14"
    BackColor="#8080FF"
    Font-Name="Arial"
    Font-Size="12"
    AlternatingItemStyle-BackColor="#C0C0C0"
    AlternatingItemStyle-Font-Name="Arial"
    AlternatingItemStyle-Font-Size="11"
    BorderColor="Black"
    OnItemDataBound="DataBound">
</asp:DataGrid>
</form>
</div>

posted on 2004-07-04 02:51  Jacobhuang  阅读(599)  评论(0)    收藏  举报

导航