简体   繁体   English

C#使用接口对dataTable行进行排序

[英]C# sort rows of dataTable using interface

Here is my code for how I get data from a database and add it to a DataTable named data, I have to sort this all table by data of row "Spelionu skaicius" using interface (increasingly), but I have no idea how to realize that, can someone help with that? 这是我如何从数据库获取数据并将其添加到DataTable命名数据的代码,我必须使用接口(越来越多)按行“Spelionu skaicius”的数据对所有表进行排序,但我不知道如何实现有人可以帮忙吗? i know there is a lotof ways to sort this, but i have to make this using interface. 我知道有很多方法可以对此进行排序,但我必须使用界面进行排序。

using (FbConnection fbBD = new FbConnection(csb.ToString()))
{
    fbBD.Open();
    FbTransaction transact = fbBD.BeginTransaction();
    FbCommand sqlReq =
        new FbCommand("SELECT vardas,pavarde,sp_skaicius,spelioniu_sk FROM zaidejai WHERE atspetas='Taip'", fbBD, transact);
    using (FbDataReader r = sqlReq.ExecuteReader())
    {
        while (r.Read())
        {
            DataRow rw = dt.NewRow();
            rw["Vardas"] = r.GetString(0);
            rw["Pavarde"] = r.GetString(1);
            rw["Skaicius"] = r.GetValue(2);
            rw["Spelionu skaicius"] = r.GetValue(3);
            data.Rows.Add(rw);
        }
    }
    transact.Commit();
    fbBD.Close();
}

PS sorry for my English 对不起我的英语

use the datatable DefaultView property: 使用数据表DefaultView属性:

data.DefaultView.Sort = "Spelionu skaicius";

you can then work with data.DefaultView (a dataview) or if you need a datatable, you can do this: 然后,您可以使用data.DefaultView(数据视图)或者如果需要数据表,可以执行以下操作:

data = data.DefaultView.ToTable();

<-- EDIT --> < - 编辑 - >

The above is for regular sorting. 以上是定期排序。 If you want to use IComparable, you will have to use custom clases in stead of DataTable. 如果要使用IComparable,则必须使用自定义clases而不是DataTable。 Here 's an explanation of how. 以下是对如何解释的解释。

It's best if you let the database sort the data so instead, I would change your select to this: 最好是让数据库对数据进行排序,相反,我会将你的选择更改为:

    SELECT vardas,pavarde,sp_skaicius,spelioniu_sk 
    FROM zaidejai 
    WHERE atspetas='Taip'
    ORDER BY spelioniu_sk

Note that I used spelioniu_sk because that's the 4th column listed on your select which is then mapped to Spelionu skaicius in your datatable definition 请注意,我使用了spelioniu_sk,因为这是select中列出的第4列,然后在您的数据表定义中映射到Spelionu skaicius

If you still must sort the datatable after you've gotten the data from the database, perhaps in a different order, you can use the DataView propery of the DataTable as so: 如果在从数据库获取数据后仍然必须对数据表进行排序(可能是以不同的顺序),则可以使用DataTable的DataView原理:

data= data.DefaultView.Sort = "`Spelionu skaicius` ASC";

OR 要么

data = data.DefaultView.Sort = "`Spelionu skaicius` DESC";

Depending on the order that you need to sort by. 取决于您需要排序的顺序。

Note: I am not sure whether the above Sort Expression will work fine. 注意:我不确定上面的Sort Expression是否可以正常工作。 Since you have decided to name your column with a space in the middle, you may need to enclose the 2 words in either backquotes (as I did above) or maybe square brackets (eg [Spelionu skaicius] ASC ) 既然您决定在中间用空格命名您的列,您可能需要将两个单词括在反引号中(如上所述)或方括号(例如[Spelionu skaicius] ASC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM