Click here to Skip to main content
15,072,735 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have text files named as "S100141.asd.text".this file contain two columns data like this


wavelength S100141.asd

325 0.015082292401682
326 0.011690540571175
327 1.06575414031349E-02


like this text file we have another file named as " S100144.asd.text" and it again contain data like this :

Wavelength S100144.asd
325 1.35059549289737E-02
326 0.010981114226419
327 1.22348778870862E-02
328 1.63555124470031E-02
329 1.57389782659161E-02

I want to import the second column of every text file in a datagridview using C# coding for futher query .I'm unable to do so .ANybody please if know the code how to do it then help me?
Posted

1 solution

Hello ,

try this way..

first declare one global variable to store the lst of string (for the second colum of every text file)
List<string> lststring = new List<string>();</string></string>

Now read every textfile containing data by this way
C#
                    OpenFileDialog op = new OpenFileDialog();//to select a file 
                    op.ShowDialog();
                    string filename = op.FileName;
                    string text = File.ReadAllText(filename);
                    string[] rowcollection= text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); //split the whole text where it takes new line


//Now make one for loop where we can again split the each row  where we find white Space 
                    foreach (string item in rowcollection) 
                    {
                        try
                        {
                            string[] substring = item.Split(' ');
                            lststring.Add(substring[1]); //and finally add the substring in the global variable .

                        }
                        catch
                        {
                        }
                    }



and now finally set datagridview DataSource to
dataGridView1.DataSource = lststring. Select(x => new { Value = x }).ToList();


I think this will help you

thanks
   

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