2013-06-08 4 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()