如何:c#将字符串拆分为多维数组以填充到datagridview中



我必须从具有分隔符"~"的字符串填充我的datagridview对于行,则"|"对于每行

中的列my datagridview columns: here

下面的示例字符串
string ShoppingDetail = "Gas LPG 5Kg|6 pcs|30.000 x 6|2.000 x 6|168.000~Gas LPG 12Kg|10 pcs|40.000 x 10|2.000 x 10|380.000";

我想从上面的字符串

填充datagridview

我的代码
var data = ShoppingDetail.Split('~').Select(x => x.Split('|')).ToArray();
int height = data.GetLength(0);
int width = data.GetLength(1);
//this.DtDetail.ColumnCount = width;
for (int r = 0; r < height; r++)
{
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(this.DataGridDetails);
for (int c = 0; c < width; c++)
{
row.Cells[c].Value = data[r][c];
}
this.DataGridDetails.Rows.Add(row);
}

我会这样做,不需要把事情复杂化:

var data = ShoppingDetail.Split("~");
for (int r = 0; r < data.Length; r++)
{
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(this.DataGridDetails);
var cells = data[r].Split("|");
for (int c = 0; c < cells.Length; c++)
{
row.Cells[c].Value = cells[c];
}
this.DataGridDetails.Rows.Add(row);
}

相关内容

  • 没有找到相关文章

最新更新