Solved

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

Posted on 2013-11-27
28
643 Views
Last Modified: 2013-12-01
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
Comment
Question by:awolarczuk
  • 15
  • 12
28 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39683772
That is a pretty broad question, so what do you need help with?  

Reading a CSV file?

Loading text into a ListView?
0
 

Author Comment

by:awolarczuk
ID: 39684288
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39684383
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
 

Author Comment

by:awolarczuk
ID: 39684466
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39684554
The easiest way to search is a for loop with logic to search, and a harder way would be LINQ query.
0
 

Author Comment

by:awolarczuk
ID: 39684564
cool mate could you please give a example of this please as i am still new to c#
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39684578
What would you like an example of?
0
 

Author Comment

by:awolarczuk
ID: 39684583
logic to search, mayby mate which ever one you tink will work better for that i would like to do
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39684588
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
 

Author Comment

by:awolarczuk
ID: 39684601
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39684606
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
 

Author Comment

by:awolarczuk
ID: 39684609
ok cool how do i change that and add the results to a listview or grid view

thanks in advance
0
 

Author Comment

by:awolarczuk
ID: 39686778
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 39686953
The key to the single-line search result is in this line:

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

You should put back the ReadLine.
0
 

Author Comment

by:awolarczuk
ID: 39687508
ao this will search everything in the file and display everyting it finds ??
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39687524
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
 

Author Comment

by:awolarczuk
ID: 39687553
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39687582
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
 

Author Comment

by:awolarczuk
ID: 39687592
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39687595
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
 

Author Comment

by:awolarczuk
ID: 39687722
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
 

Author Comment

by:awolarczuk
ID: 39687723
It Stops and only list the first match and doesnt continue
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39688501
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
 

Author Comment

by:awolarczuk
ID: 39688825
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39688853
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
 

Author Comment

by:awolarczuk
ID: 39688858
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
 

Author Closing Comment

by:awolarczuk
ID: 39688860
VERY VERY GOOD WOULD ADVISE ANYONE TO GET HIS HELP
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now