ASP.net C # Sort Data Table

I am using DAL and do not know how to sort the returned data table. The case throws an error, and intellisense does not offer me a lot of help:

// Get all the specifications available to this user
Artwork.tblSpecificationsDataTable dsCommon = new Artwork.tblSpecificationsDataTable();            
 using (tblSpecificationsTableAdapter specAdapter = new tblSpecificationsTableAdapter())
 {
  specAdapter.FillByClientID(dsCommon, Master.loginData.loggedInUser.company.ID);
  }

 DataView v = dsCommon.DefaultView;
 v.Sort = "category DESC";
 dsCommon = (Artwork.tblSpecificationsDataTable)v.ToTable();

 for (int i = 0; i < dsCommon.Count; i++)
  {
   test.Text += dsCommon[i].category + " " + dsCommon[i].FlatSize + "<br />";
  }

Error without translation:

Error 3: It is not possible to implicitly convert the type 'System.Data.DataTable' to 'Artwork.tblSpecificationsDataTable. An explicit conversion exists (are you missing a listing?)

Edit

Apparently, I should sort in the query, which is fine, but I want the ORDER BY option for any of the ten fields to not create a dozen queries in the table adapter, how can I do this?

+3
source share
2 answers

, , - , SQL Server, , .

, ...

SELECT
    <field list>
FROM
    <TableName>
WHERE
    <Clause>
ORDER BY
CASE @OrderBy
    WHEN 'Field1' THEN Field1
    WHEN 'Field2' THEN Field2
    WHEN 'Field3' THEN Field3
END
+2

. MSDN, DataView.ToTable() DataTable. dsCommon.

DataView.Table, DataTable DataView.

,

dsCommon = (Artwork.tblSpecificationsDataTable)v.ToTable();

to

dsCommon = (Artwork.tblSpecificationsDataTable)v.Table();
+2

Source: https://habr.com/ru/post/1788491/


All Articles