C# 将.txt文件放入DataGridView
我有一个C# 将.txt文件放入DataGridView,c#,datagridview,text-files,C#,Datagridview,Text Files,我有一个openFileButton,单击它会打开一个如下所示的文件: RefDeg Part# Xcntr Ycntr Rot PkgStyle U6 IC-00279G 33.411 191.494 0 QFP32 U1 IC-00272G 38.011 200.644 90 BGA177 U5 IC-00273G 46.311 179.494 0 QFP40
openFileButton
,单击它会打开一个如下所示的文件:
RefDeg Part# Xcntr Ycntr Rot PkgStyle
U6 IC-00279G 33.411 191.494 0 QFP32
U1 IC-00272G 38.011 200.644 90 BGA177
U5 IC-00273G 46.311 179.494 0 QFP40
R54 EXCLUDES 36.411 173.694 0 0402_2
R71 EXCLUDES 38.236 186.994 0 0402_2
R39 EXCLUDES 38.861 188.544 90 0402_2
C23 CAP-00130G 37.911 178.854 90 0402_3
C88 CAP-00010G 52.036 179.019 0 0603_4
C89 CAP-00010G 43.561 173.744 90 0603_3
X1 XTL-00013G 49.211 204.819 0 Crystal
X2 XTL-00012G 53.061 183.469 0 Crystal
D1 LED-00011G 58.611 181.394 0 LED
U10 IC-00198G 56.661 205.744 0 SOT
IC-00173G 59.911 205.744 0 SOT23-5
U2 IC-00274G 51.786 199.044 0 VFBGA
Q1 Excludes 43.147 189.769 0 MOSFET
U4 IC-00167G 59.211 177.394 0 SOT235_2
FID1 FIDUCIAL 5.080 24.130 0 FIDUCIAL
FIDUCIAL 59.586 192.944 0 FIDUCIAL
private static DataTable OpenTextFile()
{
#if X86 // 32-bit
string _connectionStringTemplate = "Driver={{Microsoft Text Driver (*.txt; *.csv)}};Extensions=asc,csv,tab,txt;Persist Security Info=False;Dbq={0}";
#else // 64-bit
string _connectionStringTemplate = "Driver={{Microsoft Access Text Driver (*.txt, *.csv)}};Dbq={0};Extensions=asc,csv,tab,txt";
#endif
string connectionString = string.Format(_connectionStringTemplate, @"C:\Temp\");
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
string selectAll = string.Format("select * from [{0}]", Path.GetFileName("test.txt"));
using (OdbcCommand command = new OdbcCommand(selectAll, connection))
{
connection.Open();
DataTable dataTable = new DataTable("txt");
using (OdbcDataAdapter adapter = new OdbcDataAdapter(selectAll, connection))
{
//Fills dataset with the records from file
adapter.Fill(dataTable);
return dataTable;
}
}
}
}
选择并打开文件后,我希望将.txt文件行放入/导入到DataGridView
中,然后将每一列放入DataGridView
中同一行的新列中
有人知道一种快速快捷的方法吗?最简单的方法是将文本文件导入DataTable,然后通过DataSource属性将DataTable绑定到DataGridView 您的文件看起来是固定宽度或分隔的数据文件。有很多库可以帮助将这些文件读入数据表,例如在codeproject.com上 下面是如何使用上面链接的GenericParser:
// DataFilePath stores the path + file name of your data file.
using (var p = new GenericParsing.GenericParserAdapter(DataFilePath)) {
// Assumes your data file is fixed width, with the column widths given in the array.
p.ColumnWidths = new int[] { 8, 12, 9, 9, 5, 11 };
p.FirstRowHasHeader = true;
DataTable dt = p.GetDataTable();
dataGridView1.DataSource = dt;
}
请注意,您需要在项目中添加GenericParsing.dll作为引用。您可以使用数据源和Microsoft文本文件驱动程序
像这样上传文件:
RefDeg Part# Xcntr Ycntr Rot PkgStyle
U6 IC-00279G 33.411 191.494 0 QFP32
U1 IC-00272G 38.011 200.644 90 BGA177
U5 IC-00273G 46.311 179.494 0 QFP40
R54 EXCLUDES 36.411 173.694 0 0402_2
R71 EXCLUDES 38.236 186.994 0 0402_2
R39 EXCLUDES 38.861 188.544 90 0402_2
C23 CAP-00130G 37.911 178.854 90 0402_3
C88 CAP-00010G 52.036 179.019 0 0603_4
C89 CAP-00010G 43.561 173.744 90 0603_3
X1 XTL-00013G 49.211 204.819 0 Crystal
X2 XTL-00012G 53.061 183.469 0 Crystal
D1 LED-00011G 58.611 181.394 0 LED
U10 IC-00198G 56.661 205.744 0 SOT
IC-00173G 59.911 205.744 0 SOT23-5
U2 IC-00274G 51.786 199.044 0 VFBGA
Q1 Excludes 43.147 189.769 0 MOSFET
U4 IC-00167G 59.211 177.394 0 SOT235_2
FID1 FIDUCIAL 5.080 24.130 0 FIDUCIAL
FIDUCIAL 59.586 192.944 0 FIDUCIAL
private static DataTable OpenTextFile()
{
#if X86 // 32-bit
string _connectionStringTemplate = "Driver={{Microsoft Text Driver (*.txt; *.csv)}};Extensions=asc,csv,tab,txt;Persist Security Info=False;Dbq={0}";
#else // 64-bit
string _connectionStringTemplate = "Driver={{Microsoft Access Text Driver (*.txt, *.csv)}};Dbq={0};Extensions=asc,csv,tab,txt";
#endif
string connectionString = string.Format(_connectionStringTemplate, @"C:\Temp\");
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
string selectAll = string.Format("select * from [{0}]", Path.GetFileName("test.txt"));
using (OdbcCommand command = new OdbcCommand(selectAll, connection))
{
connection.Open();
DataTable dataTable = new DataTable("txt");
using (OdbcDataAdapter adapter = new OdbcDataAdapter(selectAll, connection))
{
//Fills dataset with the records from file
adapter.Fill(dataTable);
return dataTable;
}
}
}
}
然后,只需将DataTable绑定到DataGridView即可拆分行并循环所有行/列以生成DataTable:
var fileName = this.OpenFileDialog1.FileName;
var rows = System.IO.File.ReadAllLines(fileName);
Char[] separator = new Char [] {' '};
DataTable tbl = new DataTable(fileName);
if (rows.Length != 0) {
foreach (string headerCol in rows(0).Split(separator)) {
tbl.Columns.Add(new DataColumn(headerCol));
}
if (rows.Length > 1) {
for (rowIndex = 1; rowIndex < rows.Length; rowIndex++) {
var newRow = tbl.NewRow();
var cols = rows(rowIndex).Split(separator);
for (colIndex = 0; colIndex < cols.Length; colIndex++) {
newRow(colIndex) = cols(colIndex);
}
tbl.Rows.Add(newRow);
}
}
}
var fileName=this.OpenFileDialog1.fileName;
var rows=System.IO.File.ReadAllLines(文件名);
字符[]分隔符=新字符[]{''};
DataTable tbl=新的DataTable(文件名);
如果(rows.Length!=0){
foreach(行中的字符串headerCol(0).拆分(分隔符)){
tbl.Columns.Add(新数据列(headerCol));
}
如果(rows.Length>1){
对于(rowIndex=1;rowIndex
然后将此数据表用作DataGridView的数据源。您能再解释一下发生了什么吗?我从未使用过DataTable或DataGridView..:(您好@theNoobGuy,我想如果您是DataTable和GridView对象的新手,我建议您快速使用Google和Samilia,尤其是DataTable对象,因为它对于在.Net中处理数据至关重要。如果对上述代码有任何特定问题,请告诉我。在newRow获取“预期的方法、委托或事件”错误(colIndex)=cols(colIndex);我想我会向未来的用户澄清,for循环中的错误可以通过使用int声明colIndex和rowIndex以及整数来修复,当在for循环中指向它们时,使用方括号[]而不是方括号(),与行(0)一样,应该是行[0]。谢谢Tim。