2

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

region DataValidation

    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.

3
  • 1
    Pass this datatable object to JQuery and parse it then bind it. See this link live.datatables.net/fafuyeyu/55/edit function getData(cb_func) { $.ajax({ url: "/ajax/objects.txt", //Here put your ServerSide function call success: cb_func }); } it would be nice if you change the datatable to JSON Commented Jan 14, 2019 at 7:35
  • @Ratheesh let me try with this one Commented Jan 14, 2019 at 8:28
  • @Ratheesh i found an alternative way Commented Jan 15, 2019 at 4:20

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Good idea. But did u try the same with server side processing for jquery datatable?
@ChandanKumar, of course, we can do using serverside processing. Here is the advantage is my data is small size and it will create both Table heading and data in a single loop.
Dinesan : any hint how to make it serverside processing with c# datatable will be more appreciable
@ChandanKumar We can convert the data table to JSON using JavaScriptSerializer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.