tags:

views:

1352

answers:

5

Hi, i'm using sharpdevelop to program WPF application(I'm newbie to WPF i've knowledge of VB only and no C#, is it necessary to learn C# to know WPF ?).

and i want to read CSV file first. after that i want to modify and save file. any suggestions and pointers for this ?

+1  A: 

No c# is not required. WPF can be done in VB.NET as well.

You could get fancy and use link text Or VB does has a TextFieldParser just for this http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.aspx or this link link text

 Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser _
("c:\logs\bigfile")
    MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
    MyReader.Delimiters = New String() {vbTab}
    Dim currentRow As String()
    'Loop through all of the fields in the file. 
    'If any lines are corrupt, report an error and continue parsing. 
    While Not MyReader.EndOfData
        Try
            currentRow = MyReader.ReadFields()
            ' Include code here to handle the row.
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & _
            " is invalid.  Skipping")
        End Try
    End While
End Using
nportelli
A: 

To answer the first part of your quetion. Windows Presentation Foundation is a set of classes concerned with the presentation of application user interfaces. The classes that make up WPF ca be consumed from any language, including VB.NET, so learning C# is not a neccessity in order to use WPF

With regards to the CSV component of your question, as WPF is a presentation framwork it does not directly provide the ability to parse the CSV format, however CSV is a simple and common format so writing a parser, or finding an existing one shouldn't be too difficult.

While there is no formal specification for the format, Wikipedia does provide a good deal of information about it should you decide to write a parser for it yourself.

As stated above there are many pre-existing libraries, some of which are open-source icluding this one I found on code project

NB: I have not used the CSV library mentioned above, it is provided meerly as a starting point.

Crippledsmurf
+2  A: 

There is lots of code online for reading/writing CSV files. Here is a complete (and free) library: http://www.filehelpers.com/

Here is a good implementation for CSV parsing if you want to get into the source code for this type of task: http://www.codeproject.com/KB/database/CsvReader.aspx

Alex
+4  A: 

WPF is a user interface technology and I wouldn't associate it with reading a CSV file. Reading the file and displaying are two different steps. For the first step, you could use KBCsv:

Dim reader As CsvReader = Nothing
Try
    reader = New CsvReader("C:\data.csv")
    reader.ReadHeaderRecord()
    Dim record As DataRecord = reader.ReadDataRecord

    While (Not record Is Nothing)
        System.Console.WriteLine("{0} is {1} years old", record("Name"), record("Age"))
        record = reader.ReadDataRecord
    End While
Finally
    If (Not reader Is Nothing) Then
        reader.Close()
    End If
End Try

For the second step, WPF is an option. But so is Windows Forms, or just a plain console with text output. Assuming WPF, you could display the data inside a DataGrid.

HTH, Kent

Kent Boogaart
The code you posted above looks like C#. Now that i'm unaware of it, how should i proceed ?Also is it possible to work with VB.NET and WPF together. I can understand WPF is just presentation but i've no idea how to glue together both of them. any suggestions ?
Stark
I was surprised when I saw that the current .Net doesn't come with a built in data grid. That seemed like an oversight when comparing to Window Forms functionality
Peter M
@Stark: changed example to VB. Yes, you can use VB to author WPF applications. However, I'm not familiar enough with SharpDevelop to help you further. You might also consider using Visual Studio Express edition (which is free), at least while you're learning. Most examples and help out there assumes Visual Studio, so it might make your life a bit easier.
Kent Boogaart
@Peter: the linked `DataGrid` control is intended to be rolled into .NET 4. It is a shame that it has taken so long, however.
Kent Boogaart
@Kent,Thanks for help :)
Stark
@Kent - I am looking forward to 4.0 partly for that reason. And I think MS dropped the ball over not having any sort of data grid in the current .Net
Peter M
A: 

You don't need to know C# to use WPF, but there seem to be more tutorials and blogs geared towards C#. The Microsoft documentation for WPF is provided in both VB and C#.

As for dealing with a CSV file, parsing it is fairly straight-forward. What most people (in WinForm land) seem to do is parse the CSV and drop each "Column" into a DataTable. Then, a grid control was dropped on a form and populated from (or data-bound to) the DataTable. Then, after edits, the changed DataTable was read row by row and spit out again as a CSV file (I'm sure there are plenty of caveats, especially when quoting commas in strings, as just one example).

So, the difference in WPF land is that you probably don't want to take this approach. You're choosing WPF because you want to present each "row" of the CSV file in a more interesting way. Maybe you want an animated, scrolling rolodex, if each CSV row represented a contact, for example. I would start by checking out this walkthrough, because it illustrates a good design for a WPF application.

Update:

There is a DataGrid for WPF provided in the toolkit on Codeplex, but it's typically not the best choice for what WPF is designed to work with.

emptyset
Exactly, i choose WPF cause i wanted to add animation to csv data and to present it in better way. Thanks for that walk-through link.
Stark
Cool, then make sure to find a good example of how to do paging.
emptyset
@emptyset - there is no DataGrid control in WPF at the moment. You can only get it if you load an external library. I believe that it will appear in 4.0
Peter M