我正在做一个项目,我必须导入一个 CSV 文件,并在 DataGridView 中显示结果。我正在努力将我的数据字段显示到我的 datagridview 中,我希望能够一次添加每一行,以便正确解析它们。到目前为止,这是我的代码。
csv.MissingFieldAction = MissingFieldAction.ReplaceByNull;
int fieldCount = csv.FieldCount;
string[] headers = csv.GetFieldHeaders();
fieldCount = fieldCount - 1;
//TO DO: Reading Header Information
for (int i = 0; i <= fieldCount; i++)
{
DataGridViewTextBoxColumn headerRow = new DataGridViewTextBoxColumn();
headerRow.Name = headers[i];
headerRow.HeaderText = headers[i];
headerRow.Width = 100;
dgvComplianceImport.Columns.Add(headerRow);
}
while (csv.ReadNextRecord())
{
//for (int i = 0; i < fieldCount; i++)
// string.Format("{0} = {1};",
// headers[i],
// csv[i] == null ? "MISSING" : csv[i]);
//TO DO: for loop to add each data field row
DataGridViewRow dgvr = new DataGridViewRow();
for (int fieldCount = 0; fieldCount <= csv.FieldCount; fieldCount++)
{
string field = csv[fieldCount];
}
dgvr.Cells.Add(new DataGridViewCell());
dgvComplianceImport.Rows.Add(dgvr);
}
dgvComplianceImport.DataSource = csv;
}