Display CSV File with DataGrid

This code sample shows how to read a .CSV file using the OleDb Managed Provider, and then display it, using a DataGrid. For this particular sample, after downloading the Lotto Texas Results in a CSV file, I just trimmed it a little for speed and ease of viewing. If no other .CSV file is available, you can download the full file here:
Download Lottery file
To make this work on your system, just adjust the path to match your needs on your system.

Also, this sample shows how to add Header Text when the .CSV file doesn’t have any headers. — Extra Thanks to DataGrid Girl for the extra help on the header definitions, since the CSV file comes without headers. To Do this, just create a DataBound Subroutine using DatagridItemEventArgs as an argument, then refer to it in the DataGrid properties:
OnItemDataBound=”DataBound”

<%@ 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:\inetpub\wwwroot;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>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>