Click here to Skip to main content
15,902,861 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Error opening serial port that work before Pin
albchinsh8-Apr-10 5:51
albchinsh8-Apr-10 5:51 
GeneralRe: Error opening serial port that work before Pin
DaveAuld8-Apr-10 6:25
professionalDaveAuld8-Apr-10 6:25 
GeneralRe: Error opening serial port that work before Pin
albchinsh8-Apr-10 18:29
albchinsh8-Apr-10 18:29 
AnswerRe: Error opening serial port that work before Pin
Luc Pattyn8-Apr-10 20:07
sitebuilderLuc Pattyn8-Apr-10 20:07 
GeneralRe: Error opening serial port that work before Pin
albchinsh9-Apr-10 18:13
albchinsh9-Apr-10 18:13 
Questionvb.net DataView.RowFilter not working Pin
dodaddydo7-Apr-10 16:52
dodaddydo7-Apr-10 16:52 
AnswerRe: vb.net DataView.RowFilter not working Pin
Tej Aj7-Apr-10 19:45
Tej Aj7-Apr-10 19:45 
GeneralRe: vb.net DataView.RowFilter not working Pin
dodaddydo8-Apr-10 2:55
dodaddydo8-Apr-10 2:55 
_RecordTable is of Type RecordTable which is a class that I built.

Imports System.IO
Imports System.Text
Imports Microsoft.VisualBasic.FileIO

Public Class RecordTable
    Private WithEvents _DT As New DataTable
    Private _DTList As New List(Of DataTable)
    Private _ColumnNames As List(Of String)
    Private _FileName As String
    Private _TableChanged As Boolean
    Private WithEvents _DV As New DataView(_DT)

    Friend ReadOnly Property GetView As DataView
        Get
            Return _DV
        End Get
    End Property

    Friend ReadOnly Property GetTable As DataTable
        Get
            Return _DT
        End Get
    End Property

    Friend ReadOnly Property DTList As List(Of DataTable)
        Get
            Return _DTList
        End Get
    End Property

    Friend Property ColumnNames As List(Of String)
        Get
            Return _ColumnNames
        End Get
        Set(ByVal value As List(Of String))
            _ColumnNames = value
        End Set
    End Property

    Friend Property FileName As String
        Get
            Return _FileName
        End Get
        Set(ByVal value As String)
            _FileName = value
        End Set
    End Property

    Friend Property TableChanged As Boolean
        Get
            Return _TableChanged
        End Get
        Set(ByVal value As Boolean)
            _TableChanged = value
        End Set
    End Property

    Public Sub New()
        AddHandler _DT.RowChanged, New DataRowChangeEventHandler(AddressOf TableHasChanged)

    End Sub

#Region "Form Routines"

    Friend Sub ReadRecords()
        Dim recordX As Integer = 0
        Dim dr As DataRow

        Try
            _DT.Clear()

            Using parser As New TextFieldParser(FileName)
                parser.SetDelimiters("|")

                While Not parser.EndOfData
                    ' Read in the fields for the current record
                    Dim fields As String() = parser.ReadFields()
                    Dim columns As String()

                    ' Add record to data table
                    If recordX = 0 Then
                        Dim columnNames As New List(Of String)

                        columns = fields

                        For Each column As String In columns
                            columnNames.Add(column)
                            _DT.Columns.Add(column)
                        Next

                        Me.ColumnNames = columnNames

                        recordX += 1
                    Else
                        Dim i As Integer = 0

                        dr = _DT.NewRow

                        For Each field As String In fields
                            dr.Item(Me.ColumnNames(i)) = field

                            i += 1
                        Next

                        _DT.Rows.Add(dr)

                        recordX += 1
                    End If
                End While
            End Using

            _TableChanged = False

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Friend Sub SaveFileAs()
        Dim recordBuilder As New StringBuilder

        Try
            Using outputWriter As New StreamWriter(FileName, False, Encoding.UTF8)

                For Each column As DataColumn In _DT.Columns
                    If _DT.Columns.IndexOf(column.ColumnName) = 0 Then
                        recordBuilder.Append(column.ColumnName)
                    Else
                        recordBuilder.Append(String.Format("|{0}", column.ColumnName))
                    End If
                Next

                outputWriter.WriteLine(recordBuilder.ToString)
                recordBuilder.Clear()

                For Each row As DataRow In _DT.Rows
                    For Each column As DataColumn In _DT.Columns
                        If _DT.Columns.IndexOf(column.ColumnName) = 0 Then
                            recordBuilder.Append(row.Item(column.ColumnName).ToString)
                        Else
                            recordBuilder.Append(String.Format("|{0}", row.Item(column.ColumnName).ToString))
                        End If
                    Next

                    outputWriter.WriteLine(recordBuilder.ToString)
                    recordBuilder.Clear()
                Next
            End Using

            TableChanged = False

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Friend Sub SaveFile()
        Dim recordBuilder As New StringBuilder

        Try
            Using output As New FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write), _
                outputWriter As New StreamWriter(output, Encoding.UTF8)

                For Each column As DataColumn In _DT.Columns
                    If _DT.Columns.IndexOf(column.ColumnName) = 0 Then
                        recordBuilder.Append(column.ColumnName)
                    Else
                        recordBuilder.Append(String.Format("|{0}", column.ColumnName))
                    End If
                Next

                outputWriter.WriteLine(recordBuilder.ToString)
                recordBuilder.Clear()

                For Each row As DataRow In _DT.Rows
                    For Each column As DataColumn In _DT.Columns
                        If _DT.Columns.IndexOf(column.ColumnName) = 0 Then
                            recordBuilder.Append(row.Item(column.ColumnName).ToString)
                        Else
                            recordBuilder.Append(String.Format("|{0}", row.Item(column.ColumnName).ToString))
                        End If
                    Next

                    outputWriter.WriteLine(recordBuilder.ToString)
                    recordBuilder.Clear()
                Next
            End Using

            TableChanged = False

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Friend Function GetUniqueFileName()
        Dim i As Integer = 2
        Dim uniqueFileName As String = FileName

        While My.Computer.FileSystem.FileExists(uniqueFileName)
            uniqueFileName = String.Format("{0}{1}", uniqueFileName, i)

            i += 1
        End While

        Return uniqueFileName
    End Function

    Private Sub TableHasChanged(ByVal sender As Object, ByVal e As DataRowChangeEventArgs)
        TableChanged = True
    End Sub

    Friend Function CheckTableUpdates()
        Try
            If TableChanged Then
                Dim result As DialogResult = MessageBox.Show("Would you like to save your changes?", "Save Changes", MessageBoxButtons.YesNoCancel, _
                                MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)

                Return result
            Else
                Return Nothing
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Function

#End Region


End Class


The link in your other post was not really helpful. I am able to get the results I'm looking for when connecting to a SQL table but my problem is that it doesn't work correctly for this application where my datatable is built dynamically in memory from a delimited flat text file. Thank you so much for trying to help me figure this one out. I hope showing you the RecordTable object will shed some light. If you have any other questions please let me know.
AnswerRe: vb.net DataView.RowFilter not working Pin
Tej Aj7-Apr-10 20:08
Tej Aj7-Apr-10 20:08 
Questionprogress bar Pin
Daniel Engelkes6-Apr-10 17:03
Daniel Engelkes6-Apr-10 17:03 
AnswerRe: progress bar Pin
Anubhava Dimri6-Apr-10 19:45
Anubhava Dimri6-Apr-10 19:45 
AnswerRe: progress bar Pin
KreativeKai7-Apr-10 7:18
professionalKreativeKai7-Apr-10 7:18 
GeneralRe: progress bar Pin
Steven J Jowett7-Apr-10 22:54
Steven J Jowett7-Apr-10 22:54 
GeneralRe: progress bar Pin
KreativeKai9-Apr-10 6:05
professionalKreativeKai9-Apr-10 6:05 
AnswerRe: progress bar Pin
Gregory Gadow7-Apr-10 7:49
Gregory Gadow7-Apr-10 7:49 
Questionbecause it is being used by another process Pin
KreativeKai6-Apr-10 7:56
professionalKreativeKai6-Apr-10 7:56 
AnswerRe: because it is being used by another process Pin
Kschuler6-Apr-10 8:34
Kschuler6-Apr-10 8:34 
GeneralRe: because it is being used by another process Pin
KreativeKai6-Apr-10 9:21
professionalKreativeKai6-Apr-10 9:21 
AnswerRe: because it is being used by another process Pin
Gregory Gadow6-Apr-10 9:07
Gregory Gadow6-Apr-10 9:07 
GeneralRe: because it is being used by another process Pin
KreativeKai6-Apr-10 9:16
professionalKreativeKai6-Apr-10 9:16 
GeneralRe: because it is being used by another process [modified] Pin
Dave Kreskowiak6-Apr-10 10:06
mveDave Kreskowiak6-Apr-10 10:06 
GeneralRe: because it is being used by another process Pin
KreativeKai7-Apr-10 6:22
professionalKreativeKai7-Apr-10 6:22 
Questionfilesystemwatcher and linux Pin
wikus706-Apr-10 3:54
wikus706-Apr-10 3:54 
AnswerRe: filesystemwatcher and linux Pin
Dave Kreskowiak6-Apr-10 4:09
mveDave Kreskowiak6-Apr-10 4:09 
GeneralRe: filesystemwatcher and linux Pin
wikus706-Apr-10 4:19
wikus706-Apr-10 4:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.