我在文本文件中有大量的文本表,我能够读取winform应用程序中的数据,但是数据网格中有一些不规则的数据。我只需将表格原样放入我的数据网格中。我想从数据中捕获“*(3)”并使用该数字我想分割下面的数据。 Plate Id列可以有n个Id。因此根据Plate Id的X-coord和Y-coord将添加到它们。如何将这个表数据放在数据网格中而不会打扰它的结构?我需要再次从datagrid重用这些数据。
*( 3)CAR PLATE COORDINATES:
* No. Plate Plate No. X-Coord Y-Coord
* Plate Type Id Coord (in) (in)
2 'CA' 1 5 8.6250 -23.3750
32.6249 -23.3750
46.5983 120.6250
46.5983 120.6250
8.6250 120.6250
'CA' 2 5 8.6250 120.6250
46.5983 120.6250
64.6250 306.3959
59.3717 369.4359
8.6250 365.2070
'CA' 3 5 8.6250 120.6250
46.5983 120.6250
64.6250 306.3959
59.3717 369.4359
8.6250 365.2070
lbl2.Text = fdlg.FileName;
IEnumerable<String> lines = File.ReadLines(lbl2.Text);
if (lines.Count() > 0 && lines.Contains("*( 3)"))
{
foreach (var columnName in lines.FirstOrDefault().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
{
dataGridView3.Columns.Add(columnName, columnName);
while (lines.Contains(null))
break;
foreach (var cellValues in lines.Skip(1))
{
var cellArray = cellValues.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
while (lines.Contains("*("))
{
foreach (var columnName in lines.FirstOrDefault()
.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
dataGridView3.Columns.Add(columnName, columnName);
}
}
}
}
答案 0 :(得分:0)
尝试使用固定宽度列而不是拆分列
的代码using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
namespace ConsoleApplication49
{
class Program
{
const string FILENAME = @"c:\temp\test.txt";
static int[] COLUMN_WIDTHS = { 9, 7, 7, 7, 9, 9};
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("No. Plate", typeof(int));
dt.Columns.Add("Plate Type", typeof(string));
dt.Columns.Add("Plate Id", typeof(int));
dt.Columns.Add("No. Coord", typeof(int));
dt.Columns.Add("X-Coord", typeof(double));
dt.Columns.Add("Y-Coord", typeof(double));
int noPlate = 0;
string plateType = "";
int plateId = 0;
int noCoord = 0;
StreamReader reader = new StreamReader(FILENAME);
string inputLine = "";
while ((inputLine = reader.ReadLine()) != null)
{
if (!inputLine.StartsWith("*"))
{
string[] splitArray = FixedColumns(inputLine);
DataRow newRow = dt.Rows.Add();
if (splitArray[0].Trim().Length > 0)
{
noPlate = int.Parse(splitArray[0]);
}
newRow["No. Plate"] = noPlate;
if (splitArray[1].Trim().Length > 0)
{
plateType = splitArray[1];
}
newRow["Plate Type"] = plateType;
if (splitArray[2].Trim().Length > 0)
{
plateId = int.Parse(splitArray[2]);
}
newRow["Plate Id"] = plateId;
if (splitArray[3].Trim().Length > 0)
{
noCoord = int.Parse(splitArray[3]);
}
newRow["No. Coord"] = noCoord;
newRow["X-Coord"] = double.Parse(splitArray[4]);
newRow["y-Coord"] = double.Parse(splitArray[5]);
}
}
}
static string[] FixedColumns(string input)
{
string[] splitArray = new string[COLUMN_WIDTHS.Length];
int startCol = 0;
for (int i = 0; i < COLUMN_WIDTHS.Length; i++)
{
if (i < COLUMN_WIDTHS.Length - 1)
{
splitArray[i] = input.Substring(startCol, COLUMN_WIDTHS[i]).Trim();
}
else
{
splitArray[i] = input.Substring(startCol).Trim();
}
startCol += COLUMN_WIDTHS[i];
}
return splitArray;
}
}
}