Link to home
Start Free TrialLog in
Avatar of awolarczuk
awolarczukFlag for Australia

asked on

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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

That is a pretty broad question, so what do you need help with?  

Reading a CSV file?

Loading text into a ListView?
Avatar of awolarczuk

ASKER

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
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

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
The easiest way to search is a for loop with logic to search, and a harder way would be LINQ query.
cool mate could you please give a example of this please as i am still new to c#
What would you like an example of?
logic to search, mayby mate which ever one you tink will work better for that i would like to do
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

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

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

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

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

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

You should put back the ReadLine.
ao this will search everything in the file and display everyting it finds ??
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.
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
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.
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%
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.
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

It Stops and only list the first match and doesnt continue
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ???
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.
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
VERY VERY GOOD WOULD ADVISE ANYONE TO GET HIS HELP