I can not seem to delete a record from the text file taken from a datagrid.
Please see below. I know I need to bind the data and at the same time I need to delete the file in the current directory. I have tried a few methods but can not seem to bind the current list and remove that record. When I run the program I can select the record in the datagrid as a setting. Any feedback would be great. I am only using text files and not access.
See my code

Imports System.IO

Public Class frmDirectory

    Private Sub frmDirectory_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        OpenExistingDirectories()
        CurrentDirectory()
    End Sub

    Private Sub btnCreate_Click(sender As System.Object, e As System.EventArgs) Handles btnCreate.Click
        Dim newDir As String = InputBox("Enter the name of the new directory", "New Directory") & ".txt"
        If newDir <> "" Then
            If IsInFile(newDir) Then
                MessageBox.Show(newDir & " is already a directory.", "Alert")
            Else
                Dim sw As StreamWriter = File.AppendText("Directories.txt")
                sw.WriteLine(newDir)
                sw.Close()
                MessageBox.Show("New directory " & newDir & " created.", "Directory Created")
                Dim sr As StreamWriter = File.CreateText(newDir)
                txtCurrent.Text = newDir
                lstDirectories.Items.Add(newDir)
                sr.Close()
            End If
        End If
    End Sub

    Private Sub btnDisplayListing_Click(sender As System.Object, e As System.EventArgs) Handles btnDisplayListing.Click
        CurrentDirectory()
        Dim directory() As String = File.ReadAllLines(lstDirectories.SelectedItem.ToString) ' choose what file, change to a function
        Dim query = From line In directory
                   Let information = line.Split(","c)
        Let name = information(0)
        Let phoneNumber = information(1)
        Select name, phoneNumber

        dgvOutput.DataSource = query.ToList
        dgvOutput.CurrentCell = Nothing
        dgvOutput.Columns("name").HeaderText = "Name"
        dgvOutput.Columns("phoneNumber").HeaderText = "Phone Number"
    End Sub


    Private Function IsInFile(newDir As String) As Boolean
        If File.Exists("Directories.txt") Then
            Dim sr As StreamReader = File.OpenText("Directories.txt")
            Dim directory As String
            Do Until sr.EndOfStream
                directory = sr.ReadLine
                If directory = newDir Then
                    sr.Close()
                    Return True
                End If
            Loop
            sr.Close()
        End If
        Return False
    End Function

    Private Sub DisplayDirectories()
        Dim newDir As String = txtCurrent.Text
        If newDir <> "" Then
            Dim sr As StreamReader = File.OpenText("Directories.txt")
            Dim directory As String
            Do Until sr.EndOfStream
                directory = sr.ReadLine
                lstDirectories.Items.Add(directory)
            Loop
            sr.Close()
        End If
    End Sub

    Private Sub OpenExistingDirectories()
        Dim sr As StreamReader = File.OpenText("Directories.txt")
        Dim directory As String
        Do Until sr.EndOfStream
            directory = sr.ReadLine
            lstDirectories.Items.Add(directory)
        Loop
        sr.Close()
    End Sub

    Private Sub CurrentDirectory()
        txtCurrent.Text = lstDirectories.Text
    End Sub

    Private Sub dgvOutput_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvOutput.CellContentClick
        dgvOutput.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
       

    End Sub

    Private Sub btnRemoveListing_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveListing.Click
        CurrentDirectory()
        Dim directory() As String = File.ReadAllLines(lstDirectories.SelectedItem.ToString) ' choose what file, change to a function

        Dim list As New List(Of frmDirectory)
        Dim ibind As BindingSource
        ibind.DataSource = dgvOutput

        dgvOutput.DataSource = list
        Dim row As Integer
        Dim dr As DataGridViewRow
        Dim index As Integer
        index = dgvOutput.SelectedRows.Item(0).Index
        dr = dgvOutput.Rows.Item(index)

        For Each dr In dgvOutput.SelectedRows
            dgvOutput.Rows.Remove(dr)
            row = row - 1
            dgvOutput.Refresh()


        Next


    End Sub
End Class

Generally speaking, you can't do indexed access methods on straight text files. You have to treat them sequentially, which means you have to operate on the entire file.

So, one technique is to load the text file into a stand-alone ADODB.Recordset object (rather than bind it to a control), delete it from there, then overwrite the entire file from your ADODB.Recordset object when you're done. Or, don't bother with ADODB at all, load the DataGrid using straight sequential methods with a FileSystemObject and rewrite it from there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.