日期:2014-05-18 浏览次数:21073 次
DataTable tempTable = new DataTable(); string str = @"AA|BB|CC|DD|MM@@EE|FF|GG|HH|MM@@II|JJ|KK|LL|MM@@"; string[] newstr = str.Split(new string[] { "@@" }, StringSplitOptions.RemoveEmptyEntries); string[] p = newstr[0].Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries); for (int i = 1; i <= p.Count(); i++) { DataColumn dataColumn = new DataColumn(); dataColumn.DataType = typeof(string); dataColumn.ColumnName = "列名" + i.ToString(); tempTable.Columns.Add(dataColumn); } List<string> strsplit = new List<string>(); int len = 0; foreach (string s in newstr) { DataRow dr = tempTable.NewRow(); strsplit = s.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries).ToList(); len = strsplit.Count; for (int i = 1; i <= len; i++) { dr["列名" + i.ToString()] = strsplit[i - 1]; } tempTable.Rows.Add(dr); } this.dataGridView1.DataSource = tempTable;