Reputation: 575
I want to bind DataTable get from Excel Sheet to a Jquery Datatable. And my aim is to bind all the columns for the Datatable as dynamically. I have no idea how to bind the columns dynamically.
C# and Jquery is the code base
In this code, I get the data from the excel sheet as a Datatable
public DataTable DataValidation(string dataExchangeSelectedColum, string entityvalue,string filename)
{
UA patsUA = Session["PaTSUA"] as UA;
//List<DataExchangeDefinitionViewModel> dataExchangeDefinitionListVM = _mapper.MapToDataExchangeDefinitionViewModelList(_dataExchangeBusiness.ValidateDataType(dataExchangeSelectedColum, entityvalue, filename, patsUA.DBConnectionString));
DataTable dataTable = _dataExchangeBusiness.DataValidation(dataExchangeSelectedColum, entityvalue, filename, patsUA.DBConnectionString);
return dataTable;
}
#endregion DataValidation
I want to bind the above DataTable into jquery Datatable. The above Datatable may vary that is the columns vary in different situations. so columns must bind dynamically.
Upvotes: 2
Views: 1192
Reputation: 575
A small change in my controller and create a partial view and load that partial view into a div
Here is the code
Controller
#region DataValidation
public ActionResult DataValidation(string dataExchangeSelectedColum, string entityvalue,string filename)
{
UA patsUA = Session["PaTSUA"] as UA;
DataTable dataTable = null;
dataTable = _dataExchangeBusiness.DataValidation(dataExchangeSelectedColum, entityvalue, filename, patsUA.DBConnectionString);
return PartialView("_ExcelDataTable", dataTable);
}
#endregion DataValidation
Created a partial view. Here it comes to play
@model System.Data.DataTable
@using System.Data;
@{
IEnumerable<DataRow> _excelDataRowList = from dataRow in Model.AsEnumerable() select dataRow;
}
<div class="table-responsive tableScroll">
<table id="data-table-basic" class="table table-striped">
<thead>
@foreach (DataColumn col in Model.Columns)
{
<tr>
@col.Caption.ToString()
</tr>
}
</thead>
<tbody>
@foreach (DataColumn dtCol in Model.Columns)
{
<tr>
@foreach (DataRow row in _excelDataRowList)
{
<td>
@row[dtCol]
</td>
}
</tr>
}
</tbody>
</table>
</div>
I load this partial view into a div where I wanted to display the table
Upvotes: 2