Hi Thomas,
Greetings from Syncfusion support.
Query: “I want to upload a CSV and display it in a grid I don't know column name in advantce. List<string> columns”
As per your requirement we have prepared a sample to render Grid with dynamic columns based on csv file. Kindly download the sample from below
Refer the below code example for your reference
|
<SfGrid DataSource="@Orders" AllowFiltering="true" AllowPaging="true">
<GridColumns>
@foreach (var prop in ColumnNames)
{
<GridColumn Field="@prop" IsPrimaryKey="@(prop == "OrderID")"></GridColumn>
}
</GridColumns>
</SfGrid>
protected override void OnAfterRender(bool firstRender) { if (firstRender) { ExcelEngine excelEngine = new ExcelEngine(); IApplication application = excelEngine.Excel; //Set the default application version application.DefaultVersion = ExcelVersion.Excel2016; //Load the existing Excel workbook into IWorkbook FileStream inputStream = new FileStream("Sample.csv", FileMode.Open); IWorkbook workbook = application.Workbooks.Open(inputStream); IWorksheet worksheet = workbook.Worksheets[0]; // Read data from the worksheet and Export to the DataTable. table = worksheet.ExportDataTable(worksheet.UsedRange, ExcelExportDataTableOptions.ColumnNames); ConvertDataTable<Order>(table); } } private void ConvertDataTable<T>(DataTable dt) { foreach (DataRow row in dt.Rows) { dynamic x = new ExpandoObject(); x.OrderID = Convert.ToInt32(row[0]); x.CustomerID = row[1].ToString(); x.Freight = Convert.ToDouble(row[3].ToString().Split("$")[1]); x.OrderDate = Convert.ToDateTime(row[2]); Orders.Add(x); } ColumnNames = (from dc in dt.Columns.Cast<DataColumn>() select dc.ColumnName).ToList<string>(); StateHasChanged(); }
|
Refer our UG documentation for your reference
Regards,
Vignesh Natarajan