Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-11-27
28
Medium Priority
?
829 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

661 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