3 回答
TA贡献1859条经验 获得超6个赞
所以我设计了一个似乎效果很好的解决方案。我使用解析器来解析字符串并在格式化字符串后添加到数据表中,以便解析友好。只需在此处发布,以防其他人将来需要帮助。
string[] ColArray = Regex.Split(columnarray, "\t\t");
string[] DatArray = Regex.Split(dataArray, "\t\t");
DatArray = DatArray.Where(x => !string.IsNullOrEmpty(x)).ToArray();
//Join all the split string using (+)
string datstring = string.Join("+", DatArray);
//Remove the additional (+) symbol infront of the \r\n\ to match array length
datstring = Regex.Replace(datstring, @"[+\\r\\]\B", "");
DataTable d = new DataTable();
foreach (string b in ColArray{
d.Columns.Add(b);
}
// use a parser to parse through the string and add the items to your datatable
using (var reader = new StringReader(datstring))
{
TextFieldParser parser = new TextFieldParser(reader)
{ HasFieldsEnclosedInQuotes = false, Delimiters = new string[] { "+" } };
while(!parser.EndOfData)
{
var drow = d.NewRow();
drow.ItemArray = parser.ReadFields();
d.Rows.Add(drow);
}
}
DGV.Datasource = d;
- 3 回答
- 0 关注
- 158 浏览
添加回答
举报