Click here to Skip to main content
15,906,626 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Folks,

I am loading data from a Treeview (i.e. user clicks node) and a TextBox (i.e. user copies text using button click) and auto-generating number column and Date and Time column data (See Image). This data is passed to a Listview which can be altered by the user. Then, the user saves the data from Listview to a text file. Then, the data is loaded from the text file and displayed in a DataGridView so the user can manipulate the data (i.e. Filtering, etc.). However, as you can see from the image, the first 8 rows in the DGV load correctly without inserting a text Snippet in the Snippet column. But, when I load the data with a Snippet, the Date and Time data is added to a new/separate row underneath the added row. I have been trying for many hours searching on Google and debugging, etc., with no success? I include (what I think are) relevant code snippets here and hope someone can help me to resolve this problem? I am grateful for any help! :-) (Sorry, I wanted to upload an image to show the problem, but I guess that's not possible here?). The DGV columns are in the following order: Number Column = 0 (#); Mapping Column = 1; Snippet Column = 2; Date and Time column = 3.

What I have tried:

'DGV SETUP:

'Create an unbound DataGridView by declaring a column count.
        DataGridView7.ColumnCount = 4
        DataGridView7.ColumnHeadersVisible = True

        ' Set the column header style.
        Dim columnHeaderStyle As New DataGridViewCellStyle()

        columnHeaderStyle.BackColor = Color.Beige
        columnHeaderStyle.Font = New Font("Verdana", 10, FontStyle.Bold)
        DataGridView7.ColumnHeadersDefaultCellStyle = columnHeaderStyle

'Creates columns and sets column header names.
            DataGridView7.Columns(0).Name = "#"
            DataGridView7.Columns(1).Name = "Mapping Items"
            DataGridView7.Columns(2).Name = "Snippet"
            DataGridView7.Columns(3).Name = "Date and Time"

 With DataGridView7
                ' .AutoResizeColumnHeadersHeight
                .Columns("#").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader
                .Columns("Mapping Items").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .Columns("Snippet").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .Columns("Date and Time").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            End With

'SAVE LISTVIEW TO A SPECIFIC FILE:

' Creates a <CSV> File
        Using csv As New System.IO.StreamWriter("C:\Users\CurrentUser\Documents\MappingData.txt", True) 'What does True do here???
            For Each Item As ListViewItem In ListView1.Items
                csv.WriteLine(String.Format("{0};{1};{2};{3}",
                                            Item.SubItems(0).Text,
                                            Item.SubItems(1).Text,
                                            Item.SubItems(2).Text,
                                            Item.SubItems(3).Text))
            Next
        End Using

'FORM LOAD EVENT: LOAD MAPPING DGV FROM TEXT FILE:

    Private Sub LOADDGV()

        Using stream As System.IO.FileStream = System.IO.File.OpenRead("C:\Users\CurrentUser\Documents\MappingData.txt")
            Using reader As New System.IO.StreamReader(stream)

                Dim line As String = reader.ReadLine()

                While (line IsNot Nothing)

                    Dim columns = line.Split(";") 'Text separator which splits to display in DGV columns (i.e. ;:," ", etc.)
                    line = reader.ReadLine()

                    Dim index = DataGridView7.Rows.Add(line)

                    DataGridView7.Rows(index).SetValues(columns)

                End While

            End Using
        End Using

    End Sub
Posted
Updated 26-Jul-18 7:47am
v2
Comments
Alek Massey 26-Jul-18 13:42pm    
What does the generated csv file (MappingData.txt) look like in notepad?
Member 13032047 26-Jul-18 15:23pm    
Hi Alek, the csv looks like the following, but I can't spot any difference between the random text (i.e. EXAMPLE 1) which loads correctly and the other text (i.e. EXAMPLE 2)? Here are the examples:

EXAMPLE 1: (This texts loads correctly into the DGV columns)

Tom Cruise will never play James Bond, but there’s a parallel universe in which he’s basically Felix Leiter, 007’s CIA buddy, and now six films deep into his own increasingly Bond-like franchise.

1;MAPPING ITEM - Who are we?;Tom Cruise will never play James Bond, but there’s a parallel universe in which he’s basically Felix Leiter, 007’s CIA buddy, and now six films deep into his own increasingly Bond-like franchise. ;Thursday 26 July 2018, 03:45

EXAMPLE 2: (This text loads the Number column(index 0; Header #), the Mapping column(index 1), and the Snippet column(index 2) all correctly, but the Date and Time are inserted in a new row under Mapping column(index 1) instead of on the same row as the data being loaded together with it under the Date and Time column(index 3)

There is nothing like a program that really works. When you can advance in your work with the help of IT. The best thing about IT is that it is developing all the time! This morning, however, I found a problem in the program that only just found a partial solution to? I spent hours looking on the internet, but couldn't find anything that resolves the problem? The data is not being loaded into the correct DataGridView(DGV) columns? But, the partial answer I just found is that the text I was using is causing the problem?! When I used a random text from the internet, perhaps with less punctuation, the data is loaded correctly into the DGV columns?!

1;MAPPING ITEM - Who are we?;There is nothing like a program that really works. When you can advance in your work with the help of IT. The best thing about IT is that it is developing all the time! This morning, however, I found a problem in the program that I only just found a partial solution to? I spent hours looking on the internet, but couldn't find anything that resolves the problem? The data is not being loaded into the correct DataGridView(DGV) columns? But, the partial answer I just found is that the text I was using is causing the problem?! When I used a random text from the internet, perhaps with less punctuation, the data is loaded correctly into the DGV columns?! ;Thursday 26 July 2018, 04:04

1 solution

 
Share this answer
 
Comments
Member 13032047 26-Jul-18 16:43pm    
Hi Alek, checked out the Text Parser solution, but the results were the same?
Member 13032047 30-Jul-18 14:27pm    
Discovered something which I don't have a solution for. The code is apparently okay, but the text formatting throws off the insertion into the DGV columns. There appear to be 3 types of formatting that cause problems: 1. Unformatted text such as text that does not contain regular paragraphs (i.e. phrases and block text together); 2. More than one paragraph (i.e. the space between the paragraphs throws it off); 3. Selecting white spaces at the end of the paragraph. When I discovered this, I was able to consistently produce both correct and incorrect column insertion by either formatting text, selecting or nor selecting white spaces at end of paragraph, etc. I seems what I need is some way to format the text snippets to remove formatting that seems unacceptable to the DGV before/during insertion to correct this problem? Any Ideas? I'm very grateful for any help!
Alek Massey 2-Aug-18 11:07am    
Here is a method using regex.replace to filter a string of unwanted characters.
I got this from https://docs.microsoft.com/en-us/dotnet/standard/base-types/how-to-strip-invalid-characters-from-a-string

using System;
using System.Text.RegularExpressions;

public class Example
{
static string CleanInput(string strIn)
{
// Replace invalid characters with empty strings.
try {
return Regex.Replace(strIn, @"[^\w\.@-]", "",
RegexOptions.None, TimeSpan.FromSeconds(1.5));
}
// If we timeout when replacing invalid characters,
// we should return Empty.
catch (RegexMatchTimeoutException) {
return String.Empty;
}
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900