1

I need to add columns to an existing datatable by reading the data in first row of the existing datatable. And then need to add data to the new columns from a text file. For an example please consider the table given below as the existing datatable. And i have mentioned the values in the first row here (dm1, dm2,..)

Image 1

And provided below is the datatable which i require. dm4 row data and dm6 row data has been added to two new columns here

Image 2

Can someone please help me to identify a solution. Thanks in advance

private DataTable CreateDT1(string name, ListBox list)
    {            
        DataTable dt = new DataTable();          
        //there are many files in the list box
        string[] files = new string[list.Items.Count];
        for (int x = 0; x < list.Items.Count; x++)
        {
            object s = list.Items[x];
            files[x] = s.ToString();
        }

        int lineno = 0;
            //for each file in list box
            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))//filtering the line which contains a specific string"
                        {                                

                            if (i == 0)
                            {                                    
                                if (lineno == 0)//first choosen line of the first text file. 
                                {
                                    //splitting the choosen line
                                    string[] split = line.Split(',');

                                    int result = split.Length;
                                    dt.Rows.Add();
                                    //Based on the number of split length of this row i have added columns
                                    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+1] = split[x];
                                    }

                                }
                                else//splitting and adding other lines of the first text file
                                {
                                    string[] split = line.Split(',');
                                    int result = split.Length;

                                        dt.Rows.Add();
                                        for (int x = 0; x < result; x++)
                                        {
                                            dt.Rows[lineno][x + 1] = split[x];
                                        }                                                                                

                                }                                    
                            }

                            else//adding lines of other text files underneath the same columns
                            {                                    
                                if (count != 0)
                                {
                                    string[] split = line.Split(',');
                                    int result = split.Length;
                                    if (result+1 <= dt.Columns.Count)
                                    {
                                        dt.Rows.Add();
                                        for (int x = 0; x < result; x++)
                                        {
                                            dt.Rows[lineno][x + 1] = split[x];
                                        }

                                    }
                                    else//My issue occurs here when there are additional columns in the other files. Basically when the split length of a line is higher than the previous text files. Then i have to add a new column at the specific positions
                                    {                                            

                                        dt.Rows.Add();
                                        for (int x = 0; x < result; x++)
                                        {
                                            if (split[x]!=dt.Rows[0][x+1])
                                            {
                                            DataColumn dc = new DataColumn(split[x], Type.GetType("System.String"));
                                            dt.Columns.Add(dc);
                                            dt.Rows[lineno][x + 1] = split[x];
                                            }
                                        }

                                    }

                                }

                                else
                                {                                        
                                    lineno -= 1;
                                    count += 1;                                        
                                }

                              }                                

                            lineno += 1;                                
                        }

                    }

                }

            }               

        return dt;
}
hanzi_ru
  • 153
  • 5
  • 23
  • So how is the textfile and the datatable related? First row in table belongs to first line in textfile? – Tim Schmelter Jun 04 '13 at 11:30
  • http://stackoverflow.com/questions/2312966/add-new-column-and-data-to-datatable-that-already-contains-data-c-sharp?rq=1 – Francesco De Lisi Jun 04 '13 at 11:31
  • @Schmelter Yes first row is the first line in text file – hanzi_ru Jun 04 '13 at 11:33
  • @hanzi_ru: What is the delimiter, is it a csv file at all, are the columns included in a header row, are the columns complete in the text file? It's really hard to help without sample data or without seeing what you have tried. – Tim Schmelter Jun 04 '13 at 11:39
  • @Tim: My coding is bit complex. Actually there are more than one text file. I have edited my post with coding. Please refer the comments to understand. thanks – hanzi_ru Jun 05 '13 at 06:20

1 Answers1

-1

If you change the SQL query to SELECT dm1, dm2, dm3, NULL AS dm4, dm5, NULL as dm6, dm7 you'll get some fill-in-the-blank values you can use straight away

baxter
  • 42
  • 2
  • He has not mentioned that he's using a database at all. All in all this answer doesn't help to join columns from different datasources, so -1. – Tim Schmelter Jun 04 '13 at 11:49