c# 读取空行_C#通过跳过空行将4923行从文本文件添加到DataGridView
发布日期:2021-06-24 13:09:51 浏览次数:2 分类:技术文章

本文共 2717 字,大约阅读时间需要 9 分钟。

I am new to C#. I have a huge text file consisting of 4923 lines of data that I wish to add to a DataGridView. The text file has a lot of spaces and blank lines in between sentences. I want that all the blank lines and spaces be skipped and load the contents to the DataGridView. Can someone give me a solution to achieve this? Can this be achieved without using DataTables and Datasource?

If my question is not clear please let me know. There may be mistakes in my code. Please help me rectify it

Thanks

This is my code

public void textload()

{

List str = new List();

String st = "";

//Path to write contents to text file

string filename = @"E:\Vivek\contentcopy\clientlist.txt";

Form.CheckForIllegalCrossThreadCalls = false;

OpenFileDialog ofd = new OpenFileDialog();

ofd.FileName = "";

ofd.Filter = "csv files|*.csv|txt files|*.txt|All Files|*.*";

ofd.ShowDialog();

st = ofd.FileName;

if (string.IsNullOrEmpty(ofd.FileName))

return;

string[] lines = File.ReadAllLines(st);

for (int i = 0; i < lines.Length; i++)

{

listBox1.Items.Add(lines[i]);

string[] s = lines[i].Split(' ');

MessageBox.Show(s.Length.ToString());

str.Add(lines[i]);

dataGridView1.Rows.Add();

if (s[i] == null && s[i] == " ")

{

continue;

}

dataGridView1.Rows[i].Cells[i].Value=s[i];

//dataGridView1.Rows[i].Cells[10].Value = s[10];

//dataGridView1.Rows[i].Cells[11].Value = s[11];

//dataGridView1.Rows[i].Cells[12].Value = s[12];

//dataGridView1.Rows[i].Cells[13].Value = s[13];

//dataGridView1.Rows[i].Cells[14].Value = s[14];

//dataGridView1.Rows[i].Cells[15].Value = s[15];

}

File.WriteAllLines(filename, str);

dataGridView1.ReadOnly = true;

}

Talk1:

Try .Replace(' ','').ToString()

Talk2:

Where to add the above line sir?

Talk3:

Can you please show us your sample text you want to parse ? IndexOutOfBoundException is different from OutOfMemoryException The number of columns in grid and in your text files are mismatch.

Solutions1

One option is to filter out empty strings as soon as you've read it. This can be done with LINQ Where and passing condition that checks string for not being whitespace only. String.IsNullOrWhitepspace covers that (in addition it would check for null that will not happen in case of using ReadAllLines).

string[] lines = File.ReadAllLines(st)

.Where(s => !String.IsNullOrWhitespace(s))

.ToArray();

Talk1:

Thanks @ALexie ,now that I have filtered all empty strings ,how to load the contents to the DataGridView?

Talk2:

, Just a side note:. If the file is very large in size you might get OutOfMemoryException if you loads complete contents at once.

Talk3:

you're right.I am getting IndexOutOfBound Exception at dataGridView1.Rows[i].Cells[i].Value=lines[i];.Can you Please tell me how to load data to DataGridview

转载地址:https://blog.csdn.net/weixin_33001305/article/details/113892358 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:fei 正则表达式_java 学习笔记之正则表达式
下一篇:mysql刷库脚本_shell使用mysqld_multi自动做多实例从库脚本

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月05日 10时39分36秒