Putting a .txt file into a DataGridView

杀马特。学长 韩版系。学妹 提交于 2019-11-28 01:12:32

You could split the lines and loop all rows/columns to generate the 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);
        }
    }
}

Then use this DataTable as DataSource for your DataGridView.

You could use a DataSource and the Microsoft Text File Driver.

http://www.connectionstrings.com/textfile

http://www.codeproject.com/KB/database/ReadTextFile.aspx

Upload the file like this:

    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;
                    }
                }
            }
        }

Then just bind the DataTable to your DataGridView

The easiest way would be to import the text file into a DataTable and then bind the DataTable to a DataGridView via the DataSource property.

You file looks to be a fixed width or delimited data file. There are plenty of libraries that would help read such files into a DataTable, for example this one over at codeproject.com comes to mind.

Here's how you would do it with the GenericParser I linked above:

// 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;
}

Note that you'll need to add GenericParsing.dll as a reference in your project.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!