Ch08 - 1 Chapter 8 – Text Files 8.1 Managing Text Files...

Info iconThis preview shows pages 1–17. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Chapter 8 – Text Files 8.1 Managing Text Files 8.2 StreamReaders, StreamWriters, and Structured Exception Handling 8.3 XML 8.1 Managing Text Files • Preliminaries • WriteAllLines Method • Sorting a Text File • Set Operations • Seaching a CSV Text File • The OpenFileDialog Control 2 3 CSV File Format • Comma Separated Values • Records are stored on one line with a comma between each field • Example: USStates.txt Delaware,DE,1954,759000 Pennsylvania,PA,44817,12296000 New Jersey,NJ,7417,8135000 Georgia,GA,57906,7637000 ( name of state,abbreviation,area,population ) LINQ Query for USStates.txt Dim states() As String = IO. File .ReadAllLines( "USStates.txt" ) Dim query = From line In states Let data = line.Split( ","c ) Let name = data(0) Let abbr = data(1) Let area = CInt (data(2)) Let pop = CInt (data(3)) Select name, abbr, area, pop 4 DataGridView Control • Used to display a table of data determined by a LINQ query. • Found in the Data group and the All Windows Forms group of the Toolbox. 5 DataGridView for Query from USStates.txt 6 dgvStates.DataSource = query.ToList dgvStates.CurrentCell = Nothing DataGridView Headers By default the rows have blank headers and the column headers contain the names of the items in the Select clause. 7 row headers column headers DataGridView Headers (cont.) • Row headers can be deleted by setting the RowHeadersVisible property of the DataGridView control to False. • A column header can be customized with a statement such as dgvStates.Columns( "area" ).HeaderText = "Land Area" 8 Altered Headers 9 Data in Table • The data appearing in the DataGridView control can be modified by using Where and Order By clauses in the LINQ query. Or by changing the selection of items in the Select clause. • Note: The Select clause must contain two or more items in order to use a DataGridView control. 10 Modified Data Where name.StartsWith( "New" ) Order By area Descending 11 12 Sorting a Text File 1. Read data from file into a string array. 2. Use a LINQ query to sort the data. 3. Write sorted data to a new file with the WriteAllLines method. IO. File .WriteAllLines( "fileName.txt" , strArrayOrQueryName) File to Sort: AgeAtInaug.txt George Washington,57 John Adams,61 Thomas Jefferson,57 James Madison,57 . . Barack Obama,47 13 Sort AgeAtInaug.txt by Age Dim agesAtInaug() As String = IO. File .ReadAllLines( "AgeAtInaug.txt" ) Dim query = From line In agesAtInaug Let age = CInt (line.Split( ","c )(1)) Order By age Select line IO. File .WriteAllLines( "Sorted.txt" , query) 14 File Sorted.txt Theodore Roosevelt,42 John Kennedy,43 Ulysses Grant,46 Bill Clinton,46 . . Ronald Reagan,69 15 Ways to Combine Two Files • Merge (with or without duplications) • Create a file consisting of the items appearing in both files • Delete items appearing in one file from the other file The tasks above are carried out with the Set operations on arrays....
View Full Document

This note was uploaded on 01/19/2012 for the course CIT 230 taught by Professor Staff during the Fall '08 term at Oakland University.

Page1 / 59

Ch08 - 1 Chapter 8 – Text Files 8.1 Managing Text Files...

This preview shows document pages 1 - 17. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online