在asp.net c#中对数据集进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15618682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:23:38  来源:igfitidea点击:

Sorting dataset in asp.net c#

c#asp.netdataset

提问by user2208126

I'm trying to sort the data,however the names do not get sorted when I jump from one page to another.

我正在尝试对数据进行排序,但是当我从一页跳到另一页时,名称没有得到排序。

return new dao_StudentGroupStudents().GetNewStudentbyGroup(bureauId, search, groupId, currentPage, pageSize, out totalCount);

    /// <summary>
    /// Get All the students from that Bureau.
    /// </summary>

    public DataSet GetAllStudentGroupByBureau(int? GroupId, int? BureauID)
    {
        DataSet ds = new dao_StudentGroup().GetDataSet(null, null, BureauID);
        ds.Tables[0].DefaultView.Sort = "grouptitle asc";
        return ds;
    }

I'm typing this in and now I get Cannot find column columnName.

我正在输入这个,现在我得到了找不到列列名。

public DataSet GetAllStudentGroupByBureau(int? GroupId, int? BureauID) { DataSet ds = new dao_StudentGroup().GetDataSet(null, null, BureauID);

public DataSet GetAllStudentGroupByBureau(int? GroupId, int? BureauID) { DataSet ds = new dao_StudentGroup().GetDataSet(null, null, BureauID);

ds.Tables[0].DefaultView.Sort = "columnName ASC";

DataTable dt = ds.Tables[0].DefaultView.ToTable();

return ds;

}

}

采纳答案by pala?н

Try this:

尝试这个:

DataSet ds = new dao_StudentGroup().GetDataSet(null, null, BureauID);
ds.Tables[0].DefaultView.Sort = "grouptitle asc";
ds.Tables[0] = ds.Tables[0].DefaultView.ToTable();
return ds;

UPDATE

更新

You can simply do this then:

你可以简单地这样做:

public DataSet GetAllStudentGroupByBureau(int ? GroupId, int ? BureauID) 
{
    DataSet ds = new dao_StudentGroup().GetDataSet(null, null, BureauID);

    ds.Tables[0].DefaultView.Sort = "grouptitle asc";        
    DataTable dt = ds.Tables[0].DefaultView.ToTable();
    ds.Tables[0].Rows.Clear();
    foreach (DataRow row in dt.Rows)
       ds.Tables[0].Rows.Add(row.ItemArray);

    return ds;
}

回答by jiiri

You could try this:

你可以试试这个:

    //convert DataSet table to DataView  
    DataView dv = ds.Tables[0].DefaultView; 

    //apply the sort   
    dv.Sort = "grouptitle ASC"; 

    //save back into our dataset  
    ds.Tables[0] = dv.ToTable();  

回答by Umesh

You can try the following. Remember, you can't assign value to ds.Table[] back. Hence you need to create a new datatable and add it to either new or existing dataset

您可以尝试以下操作。请记住,您不能将值赋给 ds.Table[] 回来。因此,您需要创建一个新的数据表并将其添加到新的或现有的数据集

    ds.Tables[0].DefaultView.Sort = "columnName ASC";

    DataTable dt = ds.Tables[0].DefaultView.ToTable();

回答by vishal sonekar

DataView dv = ds.Tables[0].DefaultView;

DataView dv = ds.Tables[0].DefaultView;

dv.Sort = "grouptitle ASC"; 

ds = dv.ToTable();  

try these

试试这些

default it comes in ds.table[0] no need to menstion it

默认它在 ds.table[0] 中,无需提及