根据文本文件中的数据在现有列之间添加新列 [英] add new column inbetween existing columns according to data in a text file

查看:80
本文介绍了根据文本文件中的数据在现有列之间添加新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据下面显示的方法将文本文件中的数据添加到数据表:假设这些是我在第一个文本文件中使用的行:

data1,data2

1,2

假设这些是我在第二个文本文件中使用的行:

data1,data2

3,4

我会将它们插入到数据表中,如下所示:

data1 data2

1 2

3 4

到目前为止,我已完成编码并且成功运行。当第3个文本文件包含如下所示的数据时会出现问题:

data1,data0,data2

5,6,7

此处实例我需要重新安排我的数据表,如下所示:

data1 data0 data2

1 2

3 4

5 6 7

我尝试了很多方法,但它一直只在数据表的末尾添加数据。有人可以帮忙吗我在下面提供了我的编码:



Hi, I'm adding data in text files to a datatable according to the method shown below: suppose these are the lines that i use in the 1st text file:
data1, data2
1,2
suppose these are the lines that i use in the 2nd text file:
data1, data2
3,4
I would insert them to a datatable as show below:
data1 data2
1 2
3 4
I have done the coding upto this point and it works successfully. The issue occurs when the 3rd text file contains data as shown below:
data1, data0, data2
5,6,7
at this instance i need to re arrange my datatable as shown below:
data1 data0 data2
1 2
3 4
5 6 7
I tried many ways but it keeps on adding data only to the end of the datatable. Can somebody please help. I have provided my coding below:

private DataTable CreateDT1(string name, ListBox list)
    {            
        DataTable dt = new DataTable();          
        string[] files = new string[list.Items.Count];//text files are taken to a list box
        for (int x = 0; x < list.Items.Count; x++)
        {
            object s = list.Items[x];
            files[x] = s.ToString();
        }            
        int lineno = 0;                
            for (int i = 0; i < files.Length; i++)
            {
                int count = 0;  
                string file = files[i];
                using (System.IO.StreamReader sr = new System.IO.StreamReader(file))
                {                                          
                    string line;                   
                    while ((line = sr.ReadLine()) != null)
                    {                            
                        if (line.Contains(name))//i'm filtering some lines containing a given string
                        {                                         
                            if (i == 0)
                            {                                    
                                if (lineno == 0)//first line in the first test file. Eg: data1,data2
                                {
                                    string[] split = line.Split(',');//splitting the line adding data to rows

                                    int result = split.Length;
                                    dt.Rows.Add();                                        
                                    for (int x = 0; x < result; x++)
                                    {
                                        DataColumn dc = new DataColumn(x.ToString(), Type.GetType("System.String"));
                                        dt.Columns.Add(dc);                                            
                                        dt.Rows[lineno][x] = split[x];
                                    }                                                                            
                                }
                                else
                                {
                                    string[] split = line.Split(',');//splitting the other lines in 1st text file and adding data to rows
                                    int result = split.Length;
                                    dt.Rows.Add();
                                        for (int x = 0; x < result; x++)
                                        {
                                            dt.Rows[lineno][x] = split[x];
                                        }                                                                            
                                }                                    
                            }

                            else
                            {                                    
                                if (count != 0)//for other (2nd, 3rd, ..)text files. This refers to the lines other than the 1st line(5,6,7). First line (data1, data0, data2)of these text files are not accounted here.
                                {
                                    string[] split = line.Split(',');
                                    int result = split.Length;                                        
                                        dt.Rows.Add();
                                        for (int x = 0; x < result; x++)
                                        {
                                            dt.Rows[lineno][x] = split[x];
                                        }                                           
                                }

                                else //this is the first line of other files
                                {
                                    string[] split = line.Split(',');
                                    int result = split.Length;
                                    if (result + 1 > dt.Columns.Count)//if existing number of columns are less than the split result of the first line, add new columns accordingly. I need help at this point
                                    {                                            
                                        for (int x = 0; x < result; x++)
                                        {
                                            Object a = dt.Rows[0][x];
                                            if (split[x] == Convert.ToString(a))
                                            {
                                                dt.Rows[0][x] = split[x];
                                            }
                                            else
                                            {
                                                dt.Columns.Add(x.ToString() + split[x]).SetOrdinal(x + 1);
                                                //dt.Columns.Add(x.ToString() + split[x]).SetOrdinal(x);
                                                dt.Rows[0][x.ToString() + split[x]] = split[x];
                                            }                                                
                                        }
                                        lineno -= 1;
                                        count += 1;
                                    }
                                    else
                                    {
                                        lineno -= 1;
                                        count += 1;
                                    }
                                }
                              }                                
                            lineno += 1;                                
                        }
                    }
                }
            }               
        return dt;
    }

推荐答案

为什么需要修改列顺序.Datatable对用户不可见。如果你想要要通过datagridview显示内容,您可以使用Column.DisplayIndex属性。
Why do you need to modify the column order.Datatable is not visible to user.Now if you want to display the content through a datagridview then you can use Column.DisplayIndex property.


这篇关于根据文本文件中的数据在现有列之间添加新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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