【问题标题】:Sorting dataset in asp.net c#在asp.net c#中对数据集进行排序
【发布时间】:2013-07-15 14:12:48
【问题描述】:

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

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;
    }

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

公共数据集 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;

}

【问题讨论】:

  • 这里有问题吗?
  • 那么它在第一页上的排序正确吗?

标签: c# asp.net dataset


【解决方案1】:

试试这个:

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;

更新

你可以简单地这样做:

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;
}

【讨论】:

  • ds.Tables[0] 产生错误。属性或索引 system.Datadatatablescollection.this[int] 不能赋值是只读的
  • 我现在收到此错误 错误 6 'BusinessObjects.StudentGroupManager' 没有实现接口成员 'BusinessObjects.Contracts.ISTudentGroupManager.GetAllStudentGroupByBureau(int?, int?)'。 'BusinessObjects.StudentGroupManager.GetAllStudentGroupByBureau(int?, int?)' 无法实现 'BusinessObjects.Contracts.IStudentGroupManager.GetAllStudentGroupByBureau(int?, int?)' 因为它没有匹配的返回类型 'System.Data.DataSet'。 C:\workspace\DOP.IT.Training\Development\MS\BusinessObjects\StudentGroupManager.cs 11 18 BusinessObjects
  • DataTable dt 中的第二个“=”给了我一个无效的表达式术语和 ds.给了我一个“;预期”
  • @user2208126:抱歉,这是一个打字错误。我已经更新了代码。试一次!
【解决方案2】:

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

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

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

【讨论】:

  • = 给了我一个无效的表达术语
  • 我现在收到此错误 错误 6 'BusinessObjects.StudentGroupManager' 没有实现接口成员 'BusinessObjects.Contracts.ISTudentGroupManager.GetAllStudentGroupByBureau(int?, int?)'。 'BusinessObjects.StudentGroupManager.GetAllStudentGroupByBureau(int?, int?)' 无法实现 'BusinessObjects.Contracts.IStudentGroupManager.GetAllStudentGroupByBureau(int?, int?)' 因为它没有匹配的返回类型 'System.Data.DataSet'。 C:\workspace\DOP.IT.Training\Development\MS\BusinessObjects\StudentGroupManager.‌​cs 11 18 BusinessObjects
  • 我能够编译上述代码而没有任何错误。通过查看上面的代码,似乎还有其他原因导致了问题。
  • 我把它放进去,我得到找不到列 columnName。公共数据集 GetAllStudentGroupByBureau(int?GroupId, int? BureauID) { DataSet ds = new dao_StudentGroup().GetDataSet(null, null, BureauID); ds.Tables[0].DefaultView.Sort = "columnName ASC";数据表 dt = ds.Tables[0].DefaultView.ToTable();返回 ds; }
  • 显然 columnName 表示您要排序的字段的名称。您需要将其更改为原始列名,仅供参考/示例
【解决方案3】:

你可以试试这个:

    //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();  

【讨论】:

  • ds.Tables[0] 产生错误。属性或索引 system.Datadatatablescollection.this[int] 不能赋值是只读的
【解决方案4】:

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

dv.Sort = "grouptitle ASC"; 

ds = dv.ToTable();  

试试这些

默认在 ds.table[0] 中,无需赘述

【讨论】:

    猜你喜欢
    • 2011-06-17
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    • 1970-01-01
    相关资源
    最近更新 更多