# .NET and More > WPF, WCF, WF >  [RESOLVED] DataGrid load with delimited text file quote.

## karlosc

Good evening friends of the forum, I have the following code written in a form windows forms which it works perfectly.
The code does is display in a DataGridView a text file delimited by quotation. 
Example:

'09 ' ' Manuel jury       ' '1967' '44 years'

'09 ' ' Samuel jimenez ' '1947' '65 years'


Now I want to implement a WPF windows form but not how you start, I see that you also have to implentar xaml.

here the code in windows forms.

Private Sub loadMenuItem_Click(sender As Object, e As EventArgs) Handles loadMenuItem.Click
        Try
            If (loadMenuItem.Text <> String.Empty) Then
                Dim read As New StreamReader("D:\Program Files\text\text.dat")
                Dim text As String = ""
                Dim count As Integer = 9
                Dim split As String() = Nothing
                While (Not text Is Nothing)
                    text = read.ReadLine
                    If (Not text Is Nothing) Then
                        split = text.Split(New Char() {CChar("'")}, count)
                        DataGridView1.Rows.Add(split(1), split(3), split(5), split(7), split(9))
                    End If
                End While
            Else
                MsgBox("Select the file to load", MsgBoxStyle.Exclamation, "Error")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

I would appreciate if you have something similar or help me pass this code to WPF windows

thanks,

----------


## karlosc

Hey Some partner forum will be able to help me with some example of how carrying a text file in datagrid

----------


## KGComputers

Hi,

The parsing of text.data binary file remains the same. The logic will change when manipulating the WPF Datagrid.
Make sure your familiar with how a WPF Datagrid works and Observable collection. Here's a demo I got from MSDN Forums. 
Just replace this with your code. By the way, im using c#, so just convert it to VB.NET in your end.

XAML Code:


```

<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False" Height="287" CanUserAddRows="False" 
          HorizontalAlignment="Left" Margin="12,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="479">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Split1" Binding="{Binding Split1}" Width="90"/>
                <DataGridTextColumn Header="Split3" Binding="{Binding Split3}" Width="90"/>
                <DataGridTextColumn Header="Split5" Binding="{Binding Split5}" Width="100"/>
                <DataGridTextColumn Header="Split7" Binding="{Binding Split7}" Width="100"/>
                <DataGridTextColumn Header="Split9" Binding="{Binding Split9}" Width="91"/>
            </DataGrid.Columns>
        </DataGrid>
```

Code Behind:


```

public partial class MainWindow : Window
    {
        ObservableCollection<Split> SplitRecords { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            
            StoreDatacontext();
        }

        private void StoreDatacontext()
        {
            /*
                REPLACE THE CODE BELOW WITH READING text.data 
            
                While (Not text Is Nothing)
                text = read.ReadLine
                If (Not text Is Nothing) Then
                split = text.Split(New Char() {CChar("'")}, count)
                DataGridView1.Rows.Add(split(1), split(3), split(5), split(7), split(9))
            */
            SplitRecords = new ObservableCollection<Split>()
            {
                new Split(){ Split1 = "Split1", Split3 = "Split1", Split5 = "Split1", Split7="Split1", Split9="Split1"},
                new Split(){ Split1 = "Split11", Split3 = "Split11", Split5 = "Split11", Split7="Split11", Split9="Split11"},
                new Split(){ Split1 = "Split111", Split3 = "Split111", Split5 = "Split111", Split7="Split111", Split9="Split111"},
            };

            this.DataContext = SplitRecords;
        }

        class Split
        {
            public string Split1 { get; set; }
            public string Split3 { get; set; }
            public string Split5 { get; set; }
            public string Split7 { get; set; }
            public string Split9 { get; set; }
        }
    }
```


Cheers!

KG

----------


## karlosc

thank you very much for your help, I tell you what i am doing.

----------


## KGComputers

Sure...

The conversion part to vb.net is only the tricky part...  :Smilie: 

Greg

----------


## karlosc

I can turn the code, this is the result.

Public Partial Class MainWindow
	Inherits Window
	Private Property SplitRecords() As ObservableCollection(Of Split)
		Get
			Return m_SplitRecords
		End Get
		Set
			m_SplitRecords = Value
		End Set
	End Property
	Private m_SplitRecords As ObservableCollection(Of Split)

	Public Sub New()
		InitializeComponent()

		StoreDatacontext()
	End Sub

	Private Sub StoreDatacontext()

		SplitRecords = New ObservableCollection(Of Split)() From { _
			New Split() With { _
				Key .Split1 = "Split1", _
				Key .Split3 = "Split1", _
				Key .Split5 = "Split1", _
				Key .Split7 = "Split1", _
				Key .Split9 = "Split1" _
			}, _
			New Split() With { _
				Key .Split1 = "Split11", _
				Key .Split3 = "Split11", _
				Key .Split5 = "Split11", _
				Key .Split7 = "Split11", _
				Key .Split9 = "Split11" _
			}, _
			New Split() With { _
				Key .Split1 = "Split111", _
				Key .Split3 = "Split111", _
				Key .Split5 = "Split111", _
				Key .Split7 = "Split111", _
				Key .Split9 = "Split111" _
			} _
		}

		Me.DataContext = SplitRecords
	End Sub

	Private Class Split
		Public Property Split1() As String
			Get
				Return m_Split1
			End Get
			Set
				m_Split1 = Value
			End Set
		End Property
		Private m_Split1 As String
		Public Property Split3() As String
			Get
				Return m_Split3
			End Get
			Set
				m_Split3 = Value
			End Set
		End Property
		Private m_Split3 As String
		Public Property Split5() As String
			Get
				Return m_Split5
			End Get
			Set
				m_Split5 = Value
			End Set
		End Property
		Private m_Split5 As String
		Public Property Split7() As String
			Get
				Return m_Split7
			End Get
			Set
				m_Split7 = Value
			End Set
		End Property
		Private m_Split7 As String
		Public Property Split9() As String
			Get
				Return m_Split9
			End Get
			Set
				m_Split9 = Value
			End Set
		End Property
		Private m_Split9 As String
	End Class
End Class

----------


## KGComputers

Hi,

The next thing is to replace the code:



```
SplitRecords = New ObservableCollection(Of Split)() From { _
New Split() With { _
Key .Split1 = "Split1", _
Key .Split3 = "Split1", _
Key .Split5 = "Split1", _
Key .Split7 = "Split1", _
Key .Split9 = "Split1" _
}, _
New Split() With { _
Key .Split1 = "Split11", _
Key .Split3 = "Split11", _
Key .Split5 = "Split11", _
Key .Split7 = "Split11", _
Key .Split9 = "Split11" _
}, _
New Split() With { _
Key .Split1 = "Split111", _
Key .Split3 = "Split111", _
Key .Split5 = "Split111", _
Key .Split7 = "Split111", _
Key .Split9 = "Split111" _
} _
```

To:



```
SplitRecords = New ObservableCollection(Of Split)
While (Not text Is Nothing)
  text = read.ReadLine
  If (Not text Is Nothing) Then
      split = text.Split(New Char() {CChar("'")}, count)	  
	  ' add Split object to SplitCollection
      SplitRecords.Add(New Split() With { _
			Key .Split1 = split(1), _
			Key .Split3 = split(3), _
			Key .Split5 = split(5), _
			Key .Split7 = split(7), _
			Key .Split9 = split(9) _
			})
   End If
End While

' bind splitrecords to datagrid
Me.DataContext = SplitRecords
```

As a precaution, my code might have syntax errors but that should keep you going!  :Smilie: 


Can you enclose your code snippets with code tags? Thanks

Greg

----------


## karlosc

Everything went great Greg.
Now I can load the text in datagrid edit one looking back Colomna and save.
Thanks in advance
regards Greg

Imports System.IO
Imports System.Windows.Forms
Imports System.Collections.ObjectModel

Public Partial Class MainWindow
    Inherits Window
Private Property SplitRecords() As ObservableCollection(Of Split)
    Get
        Return m_SplitRecords
    End Get
    Set(value As ObservableCollection(Of Split))
        m_SplitRecords = value
    End Set
End Property
Private m_SplitRecords As ObservableCollection(Of Split)

Public Sub New()
    InitializeComponent()

    StoreDatacontext()
End Sub

Private Sub StoreDatacontext()
    Dim leer As New StreamReader("D:\Archivos de programa\m1\prueba.dat")
    Dim texto As String = ""
    Dim count As Integer = 11
    Dim split As String() = Nothing
    SplitRecords = New ObservableCollection(Of Split)
    While (Not texto Is Nothing)
        texto = leer.ReadLine
        If (Not texto Is Nothing) Then
            split = texto.Split(New Char() {CChar("'")}, count)
            ' add Split object to SplitCollection
                SplitRecords.Add(New Split() With { _
              .Split1 = split(1), _
              .Split3 = split(3), _
              .Split5 = split(5), _
              .Split7 = split(7), _
              .Split9 = split(9) _
 })
        End If
    End While

    Me.DataContext = SplitRecords
End Sub

Private Class Split
    Public Property Split1() As String
        Get
            Return m_Split1
        End Get
        Set(value As String)
            m_Split1 = value
        End Set
    End Property
    Private m_Split1 As String

    Public Property Split3() As String
        Get
            Return m_Split3
        End Get
        Set(value As String)
            m_Split3 = value
        End Set
    End Property
    Private m_Split3 As String

    Public Property Split5() As String
        Get
            Return m_Split5
        End Get
        Set(value As String)
            m_Split5 = value
        End Set
    End Property
    Private m_Split5 As String

    Public Property Split7() As String
        Get
            Return m_Split7
        End Get
        Set(value As String)
            m_Split7 = value
        End Set
    End Property
    Private m_Split7 As String

    Public Property Split9() As String
        Get
            Return m_Split9
        End Get
        Set(value As String)
            m_Split9 = value
        End Set
    End Property
    Private m_Split9 As String

End Class
End Class

----------


## KGComputers

Your welcome!

Glad that i helped you.

Cheers!

 :Smilie: 

KG

----------


## karlosc

Hi,

Thank you.
it makes me sad to bother you much, but asked if i could help me or to export to generate the text file. In WF I keep it that way.
or i hve to create another post.



Cheers

----------


## KGComputers

Create another post and specify the details. If were busy, pretty sure we can't attend 
to. But if were doing nothing, the experts will be wandering around..  :Smilie:

----------

