Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to import data from text file to grid view  using the blew code :

 

   
My file is like :

A B C

E F D C C 

E D D D

D P


Then I get the following error 
>Input array is longer than the number of columns in this table in c# application


What I have tried:

DataTable dt = new DataTable();
   using (System.IO.TextReader tr = File.OpenText((@"d:\\My File3.log")))
   {
       string line;
       while ((line = tr.ReadLine()) != null)
       {

           string[] items = line.Trim().Split(' ');
           if (dt.Columns.Count == 0)
           {
               // Create the data columns for the data table based on the number of items
               // on the first line of the file
               for (int i = 0; i < items.Length; i++)
                   dt.Columns.Add(new DataColumn("Column" + i, typeof(string)));
           }
           dt.Rows.Add(items);

       }
       //show it in gridview
       this.GridView1.DataSource = dt;
       this.GridView1.DataBind();
Posted
Updated 16-Apr-17 20:54pm

You state the problem in your comment, the number of columns is based on the length of the first line of the file, the second line is longer than the first and therefore there are not enough columns.

I have not tried this out and it might need a tweak but here is a rough fix;

C#
string[] items = line.Trim().Split(' ');
           if (dt.Columns.Count < items.Length)
           {
               for (int i = dt.Columns.Count; dt.Columns.Count <> items.Length; i++)
                   dt.Columns.Add(new DataColumn("Column" + i, typeof(string)));
           }
 
Share this answer
 
First u need to get max column from file and create Datatable than loop it and show in Gridview. use below code

 DataTable dt = new DataTable();
            using (System.IO.TextReader tr = File.OpenText((@"C:\Users\madhav.jain\Desktop\ss.txt")))
            {
                string line;
//add new list of string arrey
                List<string[]> lststr = new List<string[]>();
                while ((line = tr.ReadLine()) != null)
                {

                    string[] items = line.Trim().Split(' ');
                    lststr.Add(items);
                }
                int col = lststr.Max(x => x.Length);
                if (dt.Columns.Count == 0)
                {
                    // Create the data columns for the data table based on the number of items
                    // on the first line of the file
                    for (int i = 0; i < col; i++)
                        dt.Columns.Add(new DataColumn("Column" + i, typeof(string)));
                }
// loop the list 
                foreach (string[] item in lststr)
                {
                    dt.Rows.Add(item);
                }


            }
                //show it in gridview 
                this.gv.DataSource = dt;
                this.gv.DataBind();
 
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