从文本文件导入到gridview [英] Import from text file to gridview

查看:92
本文介绍了从文本文件导入到gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();

推荐答案

您在评论中说明问题,列数基于第一行的长度该文件,第二行比第一行长,因此没有足够的列。



我没试过这个,可能需要调整,但这里是一个粗略的修复;



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;

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)));
           }


首先你需要从文件中获取max column并创建Datatable而不是循环它并在Gridview中显示。使用下面的代码



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();


这篇关于从文本文件导入到gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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