Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import CSV into DataGrid

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
like image 212
OneFineDay Avatar asked Jun 30 '26 14:06

OneFineDay


1 Answers

I decided to use the BindableDataGrid from CodePlex Since the binding is being set dynamically I had to come up with a Random string generator and assign that for the binding and all is well.

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()
like image 107
OneFineDay Avatar answered Jul 02 '26 14:07

OneFineDay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!