none
Extract data from textfile and show in Datagrid in windows forms using vb.net

    Question

  • HI all

    I need to display data from textfile into DataGrid having 2 columns. The problem is that i need to split the data 

    eg

    the Textfile data is in the format

    A0 Title||Block  Title||Array Title||Treat

    A1 Test||csk  Test||File Test||Tt

    there is a combobox and split the values of A0 and A1 and store it in the combobox

    when the user select the A0 the data of A0 must be loaded into the gridview like this

    Grid[1]      Grid[2]

    Title   Block

    Title Array

    Title Treat

    please help



    MSDN Forum

    Friday, December 14, 2012 11:45 AM

Answers

  • Hello,

    One method would be to utilize TextFieldParser class as shown below.

    Public Class MyItem
        Public Property Column1 As String
        Public Property Column2 As String
        Public Property Column3 As String
        Public Property Column4 As String
        Public Sub New()
        End Sub
    End Class

    Code to load file, populate ComboBox and code to populate DataGridView based on ComboBox select on the first column

    Private MyItemList As New List(Of MyItem)
    Private Sub ReadText()
        Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("YourFileName.txt")
            MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
            MyReader.Delimiters = New String() {"||"}
            Dim currentRow As String()
            While Not MyReader.EndOfData
                Try
                    currentRow = MyReader.ReadFields()
                    MyItemList.Add(New MyItem With
                                   {
                                       .Column1 = currentRow(0),
                                       .Column2 = currentRow(1),
                                       .Column3 = currentRow(2),
                                       .Column4 = currentRow(3)
                                   }
                               )
                Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                    Console.WriteLine(ex.Message)
                End Try
            End While
        End Using
        ComboBox1.DisplayMember = "Column1"
        ComboBox1.DataSource = MyItemList
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGridView1.DataSource = (From T In MyItemList Where T.Column1 = ComboBox1.Text).ToList
    End Sub


    KSG

    Friday, December 14, 2012 12:34 PM
  • Amal :

    Hope this helps .

       Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim lines As String() = File.ReadAllLines("c:\inputdata.txt").Where(Function(x) x <> String.Empty).ToArray()
            Dim datakeys As List(Of String) = New List(Of String)
            Dim arr As String() = Nothing
            Dim painDelim As String() = Nothing
            Dim dataKey As String = String.Empty
            Dim objTable As DataTable = New DataTable
            Dim delimCount As Integer = 0
            objTable.Columns.Add(New DataColumn With {.ColumnName = "KEY", .DataType = GetType(String)})
            objTable.Columns.Add(New DataColumn With {.ColumnName = "Grid[1]", .DataType = GetType(String)})
            objTable.Columns.Add(New DataColumn With {.ColumnName = "Grid[2]", .DataType = GetType(String)})
            For Each line As String In lines
                If line.Substring(0, line.IndexOf("||")).IndexOf(" ") > 0 Then
                    dataKey = line.Substring(0, line.IndexOf(" ")).Trim()
                    datakeys.Add(dataKey)
                End If
                Dim str As String = line
                delimCount = str.Length - str.Replace("||", "").Length
                If delimCount <= 2 Then
                    arr = line.Split("||").Where(Function(x) x <> String.Empty).ToArray()
                    objTable.Rows.Add(New Object() {dataKey, arr(0).ToString.Replace(dataKey, ""), arr(1)})
                Else
                    painDelim = line.Split(" ")
                    For Each arr1 In painDelim
                        arr = arr1.Split("||").Where(Function(x) x <> String.Empty).ToArray()
                        objTable.Rows.Add(New Object() {dataKey, arr(0).ToString.Replace(dataKey, ""), arr(1)})
                    Next
                End If
            Next
            objTable.AcceptChanges()
            Me.ComboBox1.DataSource = datakeys
            DataGridView1.DataSource = objTable.DefaultView
        End Sub

    Friday, December 14, 2012 3:20 PM

All replies

  • Hello,

    One method would be to utilize TextFieldParser class as shown below.

    Public Class MyItem
        Public Property Column1 As String
        Public Property Column2 As String
        Public Property Column3 As String
        Public Property Column4 As String
        Public Sub New()
        End Sub
    End Class

    Code to load file, populate ComboBox and code to populate DataGridView based on ComboBox select on the first column

    Private MyItemList As New List(Of MyItem)
    Private Sub ReadText()
        Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("YourFileName.txt")
            MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
            MyReader.Delimiters = New String() {"||"}
            Dim currentRow As String()
            While Not MyReader.EndOfData
                Try
                    currentRow = MyReader.ReadFields()
                    MyItemList.Add(New MyItem With
                                   {
                                       .Column1 = currentRow(0),
                                       .Column2 = currentRow(1),
                                       .Column3 = currentRow(2),
                                       .Column4 = currentRow(3)
                                   }
                               )
                Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                    Console.WriteLine(ex.Message)
                End Try
            End While
        End Using
        ComboBox1.DisplayMember = "Column1"
        ComboBox1.DataSource = MyItemList
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGridView1.DataSource = (From T In MyItemList Where T.Column1 = ComboBox1.Text).ToList
    End Sub


    KSG

    Friday, December 14, 2012 12:34 PM
  • Using the code above we can go vertical also

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Item = (From T In MyItemList Where T.Column1 = ComboBox1.Text).FirstOrDefault
        If Item IsNot Nothing Then
            DataGridView1.Rows.Clear()
            DataGridView1.Rows.Add(New Object() {Item.Column1})
            DataGridView1.Rows.Add(New Object() {Item.Column2})
            DataGridView1.Rows.Add(New Object() {Item.Column3})
            DataGridView1.Rows.Add(New Object() {Item.Column4})
        End If       
    End Sub


    KSG

    Friday, December 14, 2012 12:40 PM
  • Amal :

    Hope this helps .

       Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim lines As String() = File.ReadAllLines("c:\inputdata.txt").Where(Function(x) x <> String.Empty).ToArray()
            Dim datakeys As List(Of String) = New List(Of String)
            Dim arr As String() = Nothing
            Dim painDelim As String() = Nothing
            Dim dataKey As String = String.Empty
            Dim objTable As DataTable = New DataTable
            Dim delimCount As Integer = 0
            objTable.Columns.Add(New DataColumn With {.ColumnName = "KEY", .DataType = GetType(String)})
            objTable.Columns.Add(New DataColumn With {.ColumnName = "Grid[1]", .DataType = GetType(String)})
            objTable.Columns.Add(New DataColumn With {.ColumnName = "Grid[2]", .DataType = GetType(String)})
            For Each line As String In lines
                If line.Substring(0, line.IndexOf("||")).IndexOf(" ") > 0 Then
                    dataKey = line.Substring(0, line.IndexOf(" ")).Trim()
                    datakeys.Add(dataKey)
                End If
                Dim str As String = line
                delimCount = str.Length - str.Replace("||", "").Length
                If delimCount <= 2 Then
                    arr = line.Split("||").Where(Function(x) x <> String.Empty).ToArray()
                    objTable.Rows.Add(New Object() {dataKey, arr(0).ToString.Replace(dataKey, ""), arr(1)})
                Else
                    painDelim = line.Split(" ")
                    For Each arr1 In painDelim
                        arr = arr1.Split("||").Where(Function(x) x <> String.Empty).ToArray()
                        objTable.Rows.Add(New Object() {dataKey, arr(0).ToString.Replace(dataKey, ""), arr(1)})
                    Next
                End If
            Next
            objTable.AcceptChanges()
            Me.ComboBox1.DataSource = datakeys
            DataGridView1.DataSource = objTable.DefaultView
        End Sub

    Friday, December 14, 2012 3:20 PM