在c#3.5窗口应用程序中将多个textfiles选定列导入datagridview [英] Import multiple textfiles selected columns to datagridview in c# 3.5 window application

查看:92
本文介绍了在c#3.5窗口应用程序中将多个textfiles选定列导入datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为S100141.asd.text的文本文件。这个文件包含两列这样的数据





波长S100141.asd



325 0.015082292401682

326 0.011690540571175

327 1.06575414031349E-02





像这个文本文件我们有另一个名为S100144.asd.text的文件,它再次包含这样的数据:



波长S100144.asd

325 1.35059549289737E-02

326 0.010981114226419

327 1.22348778870862E-02

328 1.63555124470031E-02

329 1.57389782659161E-02



我要导入每个第二列使用C#编码进行进一步查询的datagridview中的文本文件。我无法这样做。如果你知道代码怎么做,那么请帮助我吗?

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?

推荐答案

你好,



试试这样..



首先声明一个全局变量来存储字符串的lst(对于每个文本文件的第二列)

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

                    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
                        {
                        }
                    }







现在最终将datagridview DataSource设置为




and now finally set datagridview DataSource to

dataGridView1.DataSource = lststring. Select(x => new { Value = x }).ToList();





我认为这会对你有所帮助


谢谢



I think this will help you

thanks


这篇关于在c#3.5窗口应用程序中将多个textfiles选定列导入datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆