如何将文本文件读取/加载到Datagridview中 [英] How do I read/load a text file into my Datagridview
本文介绍了如何将文本文件读取/加载到Datagridview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,我将dataGridview单元格的值存储在一个文本文件中,这里是它的代码。
Hello there, I am storing my values of dataGridview cells in a text file, here is the code of it.
FileStream fileStream = new FileStream(@"D:\file11.txt", FileMode.Create);
TextWriter sw = new StreamWriter(@"D:\file11.txt");
int rowcount = dataGridView1.Rows.Count;
for (int i = 0; i < rowcount - 1; i++)
{
sw.WriteLine(dataGridView1.Rows[i].Cells[0].Value.ToString() + "\t" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "\t" + dataGridView1.Rows[i].Cells[2].Value.ToString() + "\t" + dataGridView1.Rows[i].Cells[3].Value.ToString() + "\t" + dataGridView1.Rows[i].Cells[4].Value.ToString());
}
sw.Close(); //Don't Forget Close the TextWriter Object(sw)
MessageBox.Show("Data Exported");
我成功将数据导出到文本文件中,现在如何将其加载回dataGridview?我无法理解。
I am successfully exporting data into a text file, now how would I load it back to dataGridview? I cant understand.
推荐答案
您好,
您似乎将数据存储为CSV格式选项卡作为分隔符。
使用CSV Reader从文件加载数据:快速CSV阅读器 [ ^ ]
向DataGridView添加数据非常简单:
Hi,
It seems that you store your data as CSV format with Tab as separator.
Use CSV Reader to load your data from file: A Fast CSV Reader[^]
Adding data to DataGridView is fairly easy:
using (CsvReader csv = new CsvReader(new StreamReader(@"D:\file11.txt"), true))
{
int fieldCount = csv.FieldCount;
while (csv.ReadNextRecord())
{
var rowIndex = dataGridView1.Rows.Add();
for (int i = 0; i < fieldCount; i++)
{
dataGridView1.Rows[rowIndex].Cells[i].Value = csv[i];
}
}
}
或:
Or:
using (var streamReader = new StreamReader(@"D:\file11.txt"))
{
while(!streamReader.EndOfStream)
{
var line = streamReader.ReadLine();
var values = line.Split('\t');
var rowIndex = dataGridView1.Rows.Add();
for (int i = 0; i < values.Length; i++)
{
dataGridView1.Rows[rowIndex].Cells[i].Value = values[i];
}
}
}
我希望你觉得这很有用:)
I hope you find this useful :)
如果您操作DataTable并使用数据表的实例进行序列化和反序列化,则会更容易。
It is easier if you operate on a DataTable instead and use an instance of the datatable to do serialize and deserialize.
DataTable dt = new DataTable("MyTable");
dt.Columns.Add("Column1", typeof(string)); // Or whatever type you want
dt.Columns.Add("Column2", typeof(string));
dt.Columns.Add("Column3", typeof(string));
dt.Columns.Add("Column4", typeof(string));
dataGridView1.DataSource = dt.DefaultView;
// Serialize
dt.AcceptChanges();
using (TextWriter tw = StreamWriter(@"D:\file11.txt"))
{
foreach (DataRow dr in dt.Rows)
{
StringBuilder line = new StringBuilder();
line.Append(dr["Column1"].ToString());
line.Append("\t");
line.Append(dr["Column2"].ToString());
line.Append("\t");
line.Append(dr["Column3"].ToString());
line.Append("\t");
line.Append(dr["Column4"].ToString());
tw.WriteLine(line.ToString());
}
}
// Deserialize
dt.Clear();
using (TextReader tr = new StreamReader(@"D:\file11.txt"))
{
string[] parts = tr.ReadLine.Split('\t');
dt.Rows.Add(parts[0], parts[1], parts[2], parts[3]);
}
dt.AcceptChanges();
请注意上面的反序列化代码非常简单并且可能发生许多错误。
它应该让你知道如何前进。
Beware that the deserialization code above is very simple and many errors can occur.
It should give you an idea about how to go forward, though.
这篇关于如何将文本文件读取/加载到Datagridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文