读取文本文件并将数据插入Datagrid [英] Reading a Text File and inserting the data into a Datagrid
本文介绍了读取文本文件并将数据插入Datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有可能从文本文件中读取行,然后将这些值插入程序中的数据网格视图中.
我目前只能保存到文本文件,但无法读取同一文件.
如果有人愿意帮助我
I would like to know if it is possible to read lines from a Text File and then inserting those values into a Data Grid View in my Program.
I am currently only able to save to a text file, but cannot read from the same file.
If any one would be so kind as to assist me
推荐答案
好,那么确切地知道您想要什么会有所帮助,但我会给您一个基本的例子. >
Well, it would help to know exactly what you want but Ill give you a basic example.
public class MyDataObject
{
public string FirstName {get; set; }
public string LastName {get; set; }
}
我们将使用上述对象.然后在您的Program.cs
中(或您想执行代码的任何地方)
We will use the above object. Then in your Program.cs
(or where ever you want to execute your code)
using (System.IO.StreamReader sr = new System.IO.StreamReader("MyFileName"))
{
List<mydataobject> myList = new List<mydataobject>();
//Do your own reading in logic here, I will do a basic example assuming that
//the first and last names are comma separated, and each entry is on a new line
string[] names = sr.ReadToEnd().split(''\n'');
foreach (string s in names)
{
string[] values = s.split('','');
myList.Add(new MyDataObject { FirstName = values[0], LastName = values[1] });
}
//Now set your data grid''s datasource to myList and call the DataBind() method and you are good to go!
}
那是我想到的最基本的方法?
That is the most basic way I can think of doing it?
这篇关于读取文本文件并将数据插入Datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文