Click here to Skip to main content
14,639,650 members
Rate this:
Please Sign up or sign in to vote.
See more:
Dear all,

I have a problem that in my files have data such as
002
Phanny
F
09876789
pkah123
Friday, January 06, 2012
Wednesday, February 01, 2012
Phnom Penh
good
0012
asdf
F
asfd
asdf
Friday, January 06, 2012
Friday, January 06, 2012
asdf
asdf

Then I want to read it to put in the list view such as
ID Name Gender Address Phone Passport StartDate EndDate Description
002 Phanny F Phnom Penh 09876889 pkah123 Friday.. wendesday.. good

Then when It read this finish, when it see the ID, it will enter the new line.

I can do it only in one column as below
002PhannyF09876789pkah123Friday, January 06, 2012Wednesday, February 01, 2012Phnom Penhgood0012asdfFasfdasdfFriday, January 06, 2012Friday, January 06, 2012asdfasdf

this result only in one line and in one column.
My code here:
string file_name = "D:\\AEU\\C# Programming\\TurismMangementSystem\\TurismMangementSystem\\test.txt";
           string textLine = "";
           System.IO.StreamReader objReader;
           objReader = new System.IO.StreamReader(file_name);
           do
           {
               //textLine = textLine + objReader.ReadLine() + "\r\n";

               textLine = objReader.ReadLine();
           } while (objReader.Peek() != -1);

           lis.SubItems.Add(textLine);
           listView1.Items.Add(lis);

Please help me.......

Thank you so much.
Posted
Updated 5-Jan-12 16:51pm
v2
Comments
Emad Al Hawary 5-Jan-12 22:50pm
   
http://msdn.microsoft.com/en-us/library/aa287535%28v=vs.71%29.aspx
Rate this:
Please Sign up or sign in to vote.

Solution 1

Try this and vote:


int counter = 0;
string line;

// Read the file and display it line by line.
System.IO.StreamReader file =
   new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
   Console.WriteLine (line);
   counter++;
}

file.Close();

// Suspend the screen.
Console.ReadLine();
   
Comments
phanny 2011 5-Jan-12 23:03pm
   
Sorry I am using Visual Studio 2010, so please write it in Visual.
Thanks
   
What does it mean -- "in Visual"?! You should understand just a bit what are you asking about.
Get my vote of 1 for the question.
--SA
phanny 2011 5-Jan-12 23:18pm
   
Microsoft Visual Studio is a software that we can write VB.net or C#.net and ASP.net.
   
All right. Voted. A 4. You see, the question is simple, but it really worth noting that there are no situations where a hard-coded path name can be useful; and a location like "c:\text.txt" is not really quite legitimate, especially with Windows 7. Some "neutral" variable like fileName, even undeclared (I mean, in a code sample) would do better. I hope I did not offend you with this little note... :-)
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 2

it looks like you need to read 9 lines to make a record there for this might help

do
int linecount=1          
do{
//textLine = textLine + objReader.ReadLine() + "\r\n";
  if(linecount % 9 != 0)
  {
       textLine+= objReader.ReadLine();
       linecount++;
  }
  else
  {
    lis.SubItems.Add(textLine);
    listView1.Items.Add(lis);
    linecount = 1;
    textLine = "";
  }
} while (objReader.Peek() != -1);
   
v2
Comments
phanny 2011 6-Jan-12 2:32am
   
An erro on line
listView1.Items.Add(lis);
I don't know why?

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100