Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 983
  • Last Modified:

How do i search a CSV file and display results in listviewbox in C#

Hi all
Just hopind someone can help me, i am waiting to search a CSV file and display the results in a listview box

thanks inadviace
0
awolarczuk
Asked:
awolarczuk
  • 15
  • 12
1 Solution
 
Bob LearnedCommented:
That is a pretty broad question, so what do you need help with?  

Reading a CSV file?

Loading text into a ListView?
0
 
awolarczukAuthor Commented:
Hi im sorry i thought it was very clear open and search for a item in a CSV file, so file name say item.crv wit a list of items item1, part2, part3
                                                 part2, part2, part3
                                                 itempart3, part2, part3

In a text box i type say item and it shows me s list of all things in the CSV file starting with item in a list view box
0
 
Bob LearnedCommented:
You can use the TextFieldParser to read CSV files:

C# TextFieldParser
http://www.dotnetperls.com/textfieldparser

using (TextFieldParser parser = new TextFieldParser("C:\\csv.txt"))
	{
	    parser.Delimiters = new string[] { "," };
	    while (true)
	    {
		string[] parts = parser.ReadFields();
		if (parts == null)
		{
		    break;
		}
		Console.WriteLine("{0} field(s)", parts.Length);
	    }
	}

Open in new window

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
awolarczukAuthor Commented:
TheLearnedOne
cool mate i got the read one working really easy thanks but the big one is searching the file

thanks for the help mate
0
 
Bob LearnedCommented:
The easiest way to search is a for loop with logic to search, and a harder way would be LINQ query.
0
 
awolarczukAuthor Commented:
cool mate could you please give a example of this please as i am still new to c#
0
 
Bob LearnedCommented:
What would you like an example of?
0
 
awolarczukAuthor Commented:
logic to search, mayby mate which ever one you tink will work better for that i would like to do
0
 
Bob LearnedCommented:
For beginners, I would go with easy to understand:

for (int i = 0; i < lines; i++)
    for (int j = 0; j < columns; j++)
        if (text[i, j] == searchText)
        {
            // Do sumpin' here.
        }

Open in new window

0
 
awolarczukAuthor Commented:
i am currently using this to search the crv file and to show the basic results to three text boxes but i would like to show the results to a listview or datagrid so if i was to write it it would show everything with this in the title
private void button5_Click(object sender, EventArgs e)
        {
            StreamReader s = new StreamReader(@"C:\adam\test.txt");
            string currentLine;
            string searchString = textBox1.Text;
            bool foundText = false;

            do
            {
                currentLine = s.ReadLine();
                if (currentLine != null)
                {
                    foundText = currentLine.Contains(searchString);
                    
                }
            }
            while (currentLine != null && !foundText);

            if (foundText)
            {
               
                
                string file = currentLine;
                string Title = file.Split(',')[0].Trim();
                string link = file.Split(',')[1].Trim();
                string pubdate = file.Split(',')[2].Trim();
                textBox2.Text = (Title);
                textBox3.Text = (link);
                textBox4.Text = (pubdate);
            }

            else
            {
                MessageBox.Show("Title" + " " + textBox1.Text + " " + "not found");

            }

Open in new window

0
 
Bob LearnedCommented:
It doesn't look like you are searching for a list of matches, since it looks like you are searching for a single item.
0
 
awolarczukAuthor Commented:
ok cool how do i change that and add the results to a listview or grid view

thanks in advance
0
 
awolarczukAuthor Commented:
ok so i changed   //currentLine = s.ReadLine(); to   currentLine = s.ReadToEnd(); but just seems t o loop all the time any help please

thanks
0
 
Bob LearnedCommented:
The key to the single-line search result is in this line:

while (currentLine != null && !foundText);

You should put back the ReadLine.
0
 
awolarczukAuthor Commented:
ao this will search everything in the file and display everyting it finds ??
0
 
Bob LearnedCommented:
You need to do something like this within a loop:

1) Read text
2) Check if text contains search string
3) Create a ListViewItem
4) Set the ListViewItem sub items
5) Add the ListViewItem to the ListView.
0
 
awolarczukAuthor Commented:
yes mate thats rigt tat what i have been asking from the start can you please help me do this i have shown you the code i have can you please help me edit this so it will do this

thanks
0
 
Bob LearnedCommented:
You said that you were a beginner, so my goal is not to write the code for you, but get you to write it yourself.  You have most of the pieces of the puzzle, you just need to put them all together in the right order.
0
 
awolarczukAuthor Commented:
hi TheLearnedOne
i do understand that but i ave written alot o the code myself it is not like i am asking you to write all of it i tink i have done the 90% of it could you please just help me do the last 10%
0
 
Bob LearnedCommented:
Well, let's see.

You have the pieces:

1) do...while loop

2) currentLine = s.ReadLine() statement

3) Check the search text:
        if (currentLine != null && currentLine.Contains(searchString))

4) Create a ListViewItem:

      var item = new ListViewItem();

5) Add sub items
    item.SubItems.Add("#1");

6) Add item to ListViewItem
    listView1.Items.Add(item);

Like I said, you have most of the pieces to the puzzle, you just have to put them together.  It is a very important skill to be able to take pseudo code, and create concrete code from it.
0
 
awolarczukAuthor Commented:
ok mate so here is where is am at the moment i have it going to a list view, but it is just looping the same item in the listview all the time and not everyitem in the crv that is matching

  private void button5_Click(object sender, EventArgs e)
        {
            StreamReader s = new StreamReader(@"C:\adam\test.txt");
            string currentLine;
            string searchString = textBox1.Text;
            bool foundText = false;

            do
            {
               currentLine = s.ReadLine();
               
                if (currentLine != null && currentLine.Contains(searchString))
                {
                    foundText = currentLine.Contains(searchString);
                    
                }
            }
            while (currentLine != null && !foundText);

            if (foundText)
            {
               
                
                string file = currentLine;
                string Title = file.Split(',')[0].Trim();
                string link = file.Split(',')[1].Trim();
                string pubdate = file.Split(',')[2].Trim();

                foreach (var feed in currentLine)
                {

                    ListViewItem lvi = new ListViewItem(Title);
                    lvi.SubItems.Add(link);
                    lvi.SubItems.Add(pubdate);
                    listView1.Items.Add(lvi);
                }
           //     textBox2.Text = (Title);
           //     textBox3.Text = (link);
             //   textBox4.Text = (pubdate);
            }

Open in new window

0
 
awolarczukAuthor Commented:
It Stops and only list the first match and doesnt continue
0
 
Bob LearnedCommented:
Here is my tweak to your existing code, that should match up with the pseudo-code that I described earlier:

private void button5_Click(object sender, EventArgs e)
        {
            StreamReader s = new StreamReader(@"C:\adam\test.txt");
            string currentLine;
            string searchString = textBox1.Text;
            bool foundText = false;

            do
            {
               currentLine = s.ReadLine();
               
                if (currentLine != null && currentLine.Contains(searchString))
                {            
                    string file = currentLine;
                    string Title = file.Split(',')[0].Trim();
                    string link = file.Split(',')[1].Trim();
                    string pubdate = file.Split(',')[2].Trim();

                    ListViewItem lvi = new ListViewItem(Title);
                    lvi.SubItems.Add(link);
                    lvi.SubItems.Add(pubdate);
                    listView1.Items.Add(lvi);
               }
            }
            while (currentLine != null);
}

Open in new window

0
 
awolarczukAuthor Commented:
cool mate tanks so muc i ad it in the wrong spot , as i would like the links to owrk i kowi  cant od it in listview would it be just as simple to put this in a gridview just change the list view stuff to grig view ???
0
 
Bob LearnedCommented:
You would have to change a little to use a DataGridView, and that would take this question in a different direction.  No one likes a never-ending question.

Here is a starting place for DataGridView display modes:

Data Display Modes in the Windows Forms DataGridView Control
http://msdn.microsoft.com/en-us/library/cd28yf6d(v=vs.100).aspx

Use the DataGridViewLinkColumn for a hyperlink column.
0
 
awolarczukAuthor Commented:
Cool mate i know i would have anothe ritem on the form that is grid view ill take a look at that you have beene great thanks mate
0
 
awolarczukAuthor Commented:
VERY VERY GOOD WOULD ADVISE ANYONE TO GET HIS HELP
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 15
  • 12
Tackle projects and never again get stuck behind a technical roadblock.
Join Now