2013-06-08 7 views
3

В winForms добавление CSV в DataGrid было довольно простым. Теперь я пытаюсь добавить это к SilverlightDataGrid. Вот моя попытка - которая дает 3 столбца Capacity|Count|Items - помните, что значения верны 83 | 83 | _ на каждой строке. Есть 83 строки, но столбцы должны быть 23 с значениями diff в каждом. Спасибо, что посмотрели и наслаждались щедростью!Импорт CSV в DataGrid

Код:

Try 
    Dim ofd As New OpenFileDialog 
    If ofd.ShowDialog Then 
    If IO.File.Exists(ofd.File.FullName) Then 
     Dim srsCol As New List(Of List(Of String)) 
     Using fs As IO.FileStream = ofd.File.OpenRead 
     Using sr As New IO.StreamReader(fs) 
      While Not sr.Peek = -1 
      srsCol.Add(New List(Of String)(sr.ReadLine.Split(","c).ToList)) 
      End While 
     End Using 
     End Using 
     dgStaff.ItemsSource = srsCol 
    End If 
    End If 
Catch ex As Exception 
    MessageBox.Show(ex.ToString) 
End Try 

ответ

2

Я решил использовать BindableDataGrid from CodePlex Поскольку связывание создается динамически, я должен был придумать генератор случайных струн и назначить, что для связывания, и все хорошо.

csvDs.Tables.Clear() 
Try 
    Dim ofd As New OpenFileDialog 
    If ofd.ShowDialog Then 
    If IO.File.Exists(ofd.File.FullName) Then 
     csvDs.Tables.Add(csvDt) 
     Using fs As IO.FileStream = ofd.File.OpenRead 
     Using sr As New IO.StreamReader(fs) 
      Dim i As Integer 
      While Not sr.EndOfStream 
      If i = 0 Then 
       Dim cols = sr.ReadLine.Split(","c) 
       For ii As Integer = 0 To cols.Count - 1 
       Dim rndValue As String = RndColName() 
       Dim col As New BindableDataGrid.Data.DataColumn(rndValue) 
       rndValues.Add(rndValue) 
       col.DataType = GetType(System.String) 
       col.Caption = ii.ToString 
       col.ReadOnly = True 
       col.AllowReorder = False 
       col.AllowResize = False 
       col.AllowSort = False 
       csvDt.Columns.Add(col) 
       AddItemsToCb(ii) 
       Next 
       Dim row As New BindableDataGrid.Data.DataRow 
       For _i As Integer = 0 To cols.Count - 1 
       Dim s As String = cols(_i).Replace("""", String.Empty) 
       row(rndValues(_i)) = s 
       csvValues.Add(s) 
       Next 
       csvDt.Rows.Add(row) 
      Else 
       Dim cols = sr.ReadLine.Split(","c) 
       Dim row As New BindableDataGrid.Data.DataRow 
       For _i As Integer = 0 To cols.Count - 1 
       row(rndValues(_i)) = cols(_i).Replace("""", String.Empty) 
       Next 
       csvDt.Rows.Add(row) 
      End If 
      i += 1 
      End While 
     End Using 
     End Using 
     dgStaff.DataSource = csvDs 
     dgStaff.DataMember = "csvTable" 
     dgStaff.DataBind()