3

In winForms adding a CSV to a DataGrid was quite easy. I am now trying to add this to a Silverlight DataGrid. Here is my attempt - which yields 3 columns Capacity|Count|Items - mind you the values are correct 83|83|_ on each row. There are 83 rows, but the columns should be 23 with diff values in each. Thanks for looking and enjoy your bounty!

Code:

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
4

1 回答 1

2

我决定使用CodePlex 的 BindableDataGrid因为绑定是动态设置的,所以我必须想出一个随机字符串生成器并将其分配给绑定,一切都很好。

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()
于 2013-06-15T20:13:30.403 回答