This is a migrated thread and some comments may be shown as answers.

How to Convert filteredDataSourceResult.data to filteredDataTable

1 Answer 576 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Peter
Top achievements
Rank 1
Peter asked on 11 Aug 2020, 12:20 PM

I have a function that runs an SQL query, creates a DataTable from it, then creates a DataSourceResult based on the DataSourceRequest parameters.

I would like create a filteredDataTable with the results of filteredDataSourceResult.

 How can I do that? My code is below:

 

public ActionResult GrabFiltersApplytoQueryProcessFilteredResults([DataSourceRequest]DataSourceRequest request){

    String sqlConnectionString = db.Database.Connection.ConnectionString;

    SqlConnection sqlConnection = new SqlConnection(sqlConnectionString); sqlConnection.Open();

    string sqlCommandText = "";

    sqlCommandText = "execute sp_query";

    SqlCommand sqlCommand = new SqlCommand(sqlCommandText, sqlConnection); SqlDataAdapter dataAdapter = new SqlDataAdapter();

    dataAdapter.SelectCommand = sqlCommand;

    DataTable dataTable = new DataTable();

    dataAdapter.Fill(dataTable);

    sqlConnection.Close();

    DataSourceResult filteredDataSourceResult = dataTable.ToDataSourceResult(request);
    //copy columns
    DataTable filteredDataTable = dataTable.Clone();
    //how do I add filteredDataSourceResult.data into filteredDataTable?
    ...
    // other processing that requires a DataTable
    ...
}

Thanks,

Peter

1 Answer, 1 is accepted

Sort by
0
Veselin Tsvetanov
Telerik team
answered on 13 Aug 2020, 09:28 AM

Hello Peter,

I will quote here my answer from the support thread you have opened on the same topic:

From the DataSourceResult object you could retrieve the filtered-out IEnumerable like so:

var data = filteredDataSourceResult.Data;

Then you should convert the IEnumerable to DataTable. Here is a StackOverflow discussion on that matter:

https://stackoverflow.com/questions/1253725/convert-ienumerable-to-datatable

Regards,
Veselin Tsvetanov
Progress Telerik

Tags
Grid
Asked by
Peter
Top achievements
Rank 1
Answers by
Veselin Tsvetanov
Telerik team
Share this question
or