Advertisement
jdelano

Untitled

Feb 19th, 2025
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.95 KB | None | 0 0
  1.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2.  
  3.  
  4.         Dim TextFieldParser1 As New TextFieldParser("D:\VS_Projects\VB_WF_DataGrid2019\VB_WF_DataGrid2019\organizations-100.txt")
  5.  
  6.         TextFieldParser1.Delimiters = New String() {","}
  7.  
  8.         While Not TextFieldParser1.EndOfData
  9.             Dim Row1 As String() = TextFieldParser1.ReadFields()
  10.  
  11.             If dgvData.Columns.Count = 0 Then
  12.                 Dim i As Integer
  13.  
  14.                 For i = 0 To Row1.Count - 1
  15.                     dgvData.Columns.Add(Row1(i), Row1(i))
  16.                 Next
  17.             Else
  18.                 dgvData.Rows.Add(Row1)
  19.             End If
  20.  
  21.         End While
  22.  
  23.         TextFieldParser1.Close()
  24.  
  25.     End Sub
  26.  
  27.     Dim prevCellContents As String
  28.  
  29.     Private Sub dgvData_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles dgvData.CellBeginEdit
  30.         prevCellContents = dgvData.CurrentCell.Value
  31.     End Sub
  32.  
  33.     Private Sub dgvData_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles dgvData.CellEndEdit
  34.  
  35.         Dim currentCellContents As String = dgvData.CurrentCell.Value
  36.         Dim rowID As String
  37.         Dim changedFieldIndex As Int16
  38.  
  39.         If currentCellContents <> prevCellContents Then
  40.             ' the cell changed
  41.             rowID = dgvData.Item(0, dgvData.CurrentCell.RowIndex).Value
  42.             changedFieldIndex = dgvData.CurrentCell.ColumnIndex
  43.  
  44.             If UpdateCSVFile(rowID, changedFieldIndex, currentCellContents) Then
  45.                 MsgBox($"You changed the value from {prevCellContents} to {currentCellContents}")
  46.             Else
  47.                 ' the save failed, put the old value back in the cell
  48.                 dgvData.CurrentCell.Value = prevCellContents
  49.             End If
  50.         Else
  51.             MsgBox("Edit has been cancelled")
  52.         End If
  53.  
  54.     End Sub
  55.  
  56.     Private Function UpdateCSVFile(recID As String, columnNumber As Int16, newValue As String) As Boolean
  57.  
  58.         Try
  59.             Dim rwNumber As Long = 0
  60.             Dim parser As New TextFieldParser("F:\temp\organizations-100.txt")
  61.             parser.Delimiters = New String() {","}
  62.  
  63.             Dim tempFile As New StreamWriter("F:\temp\temp.txt")
  64.             Dim tempFileFields As New List(Of String)
  65.             Dim foundID As Boolean
  66.  
  67.             While Not parser.EndOfData
  68.                 Dim fields As String() = parser.ReadFields()
  69.  
  70.                 ' if the record id matches, replace the value
  71.                 foundID = fields(0) = recID
  72.                 Dim fld As String
  73.                 For i = 0 To fields.Count - 1
  74.                     fld = fields(i)
  75.                     If fld.IndexOf(",") > 0 Then
  76.                         ' the field itself contains a comma, it must be surrounded by """" to write correctly
  77.                         ' with streamwriter
  78.                         If foundID And i = columnNumber Then
  79.                             ' this is the line that has the edit field value
  80.                             fld = """" & newValue & """"
  81.                         Else
  82.                             fld = """" & fld & """"
  83.                         End If
  84.                     Else
  85.                         If foundID And i = columnNumber Then
  86.                             fld = newValue
  87.                         End If
  88.                     End If
  89.                     tempFileFields.Add(fld)
  90.                 Next
  91.  
  92.                 tempFile.WriteLine(String.Join(",", tempFileFields))
  93.                 tempFileFields.Clear()
  94.             End While
  95.  
  96.             parser.Close()
  97.             tempFile.Close()
  98.  
  99.             ' rename the old file as a backup with date time stamp
  100.             ' rename the temp file as the file name
  101.             Dim dateTimeStr As String = Format(Now, "MMddyyHmmss")
  102.             My.Computer.FileSystem.RenameFile("F:\temp\organizations-100.txt", $"organizations-100-{dateTimeStr}.txt")
  103.             My.Computer.FileSystem.RenameFile("F:\temp\temp.txt", "organizations-100.txt")
  104.  
  105.             Return True
  106.         Catch ex As Exception
  107.             MsgBox($"Save failed. {ex.Message}")
  108.             Return False
  109.         End Try
  110.  
  111.     End Function
  112.  
  113.     Private Sub dgvData_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvData.CellMouseClick
  114.         Static lastSelectedCell As String
  115.  
  116.         If lastSelectedCell Is Nothing Then lastSelectedCell = ""
  117.  
  118.         Try
  119.             If lastSelectedCell.Length() > 0 Then
  120.                 ' reset the previously selected cell's background
  121.                 dgvData.Rows(lastSelectedCell.Split(",")(0)).Cells(lastSelectedCell.Split(",")(1)).Style.SelectionBackColor = Color.White
  122.  
  123.             End If
  124.             dgvData.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.SelectionBackColor = Color.Blue
  125.             lastSelectedCell = e.RowIndex.ToString & "," & e.ColumnIndex  ' remember this selection
  126.  
  127.         Catch ex As Exception
  128.             Exit Try
  129.         End Try
  130.  
  131.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement