将逗号分隔的文本加载到C#中的datagridview中 [英] Load comma delimited text into a datagridview in C#
本文介绍了将逗号分隔的文本加载到C#中的datagridview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的保存代码:
Here's my save code:
string str = "";
int row = dataGridView1.Rows.Count;
int cell = dataGridView1.Rows[1].Cells.Count;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < cell; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value == null)
{
//return directly
//return;
//or set a value for the empty data
dataGridView1.Rows[i].Cells[j].Value = "";
}
str += dataGridView1.Rows[i].Cells[j].Value.ToString() + ",";
}
}
write.WriteLine(str);
string str2 = "";
int row2 = dataGridView2.Rows.Count;
int cell2 = dataGridView2.Rows[1].Cells.Count;
for (int i = 0; i < row2; i++)
{
for (int j = 0; j < cell2; j++)
{
if (dataGridView2.Rows[i].Cells[j].Value == null)
{
//return directly
//return;
//or set a value for the empty data
dataGridView2.Rows[i].Cells[j].Value = "";
}
str2 += dataGridView2.Rows[i].Cells[j].Value.ToString() + ",";
}
}
write.WriteLine(str2);
我有什么尝试过:
这是我尝试用来打开它的代码,但它只是将整行放到第一列。
What I have tried:
Here's the code I tried to use to open it but all it does it place the entire line into the first column.
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
cell.Value = read.ReadLine();
}
}
推荐答案
尝试将CSV文件视为数据库并使用ODBC或OleDb读取文件。 CodeProject上有一些文章可以帮助你...
读取文本文件(txt,csv,日志,制表符,固定长度) [ ^ ]
使用OleDb导入文本文件(选项卡,CSV,自定义) [ ^ ]
(还有其他人)。
一旦你掌握了数据集或数据表中的信息,就可以将其用作DataSource
到您的DataGridView
Instead of using ReadLine try treating the CSV file as a database and use ODBC or OleDb to read the file. There are a few articles here on CodeProject that can help you ...
Read Text File (txt, csv, log, tab, fixed length)[^]
Using OleDb to Import Text Files (tab, CSV, custom)[^]
(There are others).
Once you have the information in a dataset or datatable then you can use that as theDataSource
to your DataGridView
这篇关于将逗号分隔的文本加载到C#中的datagridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文