Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to access a text file as data table by using c# windows application.

What I have tried:

private void button2_Click(object sender, EventArgs e)
       {
           string line;


           StreamReader file = new System.IO.StreamReader(@"C:\Users\Touhid Hasan\Desktop\audio\pro.txt");

           while ((line = file.ReadLine()) != null)
           {
               if (line.Contains(textBox2.Text))
               {
                   pro = file.ReadLine();



                   break;
                   //string tou = file.ReadLine();


               }
               pro = file.ReadLine();//search reult to pro
           }
           file.Close();



           label1.Text = pro;










           // player.URL = (path + word + ".wav");
           //  player.controls.play();

       }
   }
Posted
Updated 17-Feb-18 5:53am

1 solution

See the answers here, most are about using a CSV file: c# - Populating a dataset from a CSV file - Stack Overflow[^]
Something like this:
DataTable dtDataSource = new DataTable();
string[] fileContent = File.ReadAllLines(@"..\\Book1.csv");
//Create data table columns dynamically
string[] columns = fileContent[0].Split(',');

for (int i = 0; i < columns.Count(); i++)
{
	dtDataSource.Columns.Add(columns[i]);
}

string[] rowData = fileContent[i].Split(',');
dtDataSource.Rows.Add(rowData);

if (dtDataSource != null)
{
	dataGrid1.ItemsSource = dtDataSource.DefaultView;
}
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900