Use object[][] dataset

Hi,

I want to upload a CSV and display it in a grid.
I don't know column name in advantce.

If I have 
List<string> columns 
and 
List<List<string>> rows 
how can I display it in with a Blazor Grid component ?

Thanks

3 Replies

VN Vignesh Natarajan Syncfusion Team May 27, 2020 12:01 PM UTC

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 



RT Rémi THOMAS May 27, 2020 02:56 PM UTC

Thanks Vignesh 

I adapted it like this, completly neutral on column names

        ColumnNames = (from dc in dt.Columns.Cast<DataColumn>()
                       select dc.ColumnName).ToList<string>();

        foreach (DataRow row in dt.Rows)
        {
            ExpandoObject x = new ExpandoObject();
            var y = x as IDictionary<string, Object>;
            for (int i=0; i<ColumnNames.Count; i++)
                y.Add(ColumnNames[i], row[i].ToString());
            Orders.Add(x);
        }



VN Vignesh Natarajan Syncfusion Team May 28, 2020 05:06 AM UTC

Hi Rémi, 
 
Thanks for your suggestion.  
 
We are glad to hear that you have achieve your requirement using our solution.  
 
Kindly get back to us if you have further queries. 
 
Regards, 
Vignesh Natarajan 


Loader.
Up arrow icon