当通过方法完成时,数据表排序丢失
本文关键字:数据表 排序 完成时 方法 | 更新日期: 2023-09-27 18:15:14
我创建了2行代码来排序我的DataTable。我需要这样做几次,所以我把这两行代码移到一个简单的方法中,每次需要排序时我都会调用这个方法。
然而,当我调用这个方法来排序我的数据表时,数据表不维护排序。这里一定发生了什么我不知道也不理解的事情。
我的两行按照我的要求工作:
private void loadData()
{
DataTable scheduleData = new DataTable();
/*
code that fills the DataTable with data and creates the groupOrderBy string
*/
scheduleData.DefaultView.Sort = "SortDate, Finish";
scheduleData = scheduleData.DefaultView.ToTable();
scheduleData.Columns.RemoveAt(0); // at this point, when I view the
// DataTable scheduleData, the rows are sorted correctly
}
当我在上面的行运行之后查看scheduleData
DataTable时,它看起来像我想要的那样(行按正确的顺序排列)。
因此,我尝试将它们移动到如下所示的方法中:
private void loadData()
{
DataTable scheduleData = new DataTable();
/*
code that fills the DataTable with data and creates the groupOrderBy string
*/
sortDT(scheduleData, "SortDate, Finish")
scheduleData.Columns.RemoveAt(0); // at this point, when I view the
// DataTable scheduleData, the rows are no longer sorted
// (they are back to their original positions)
}
private void sortDT(DataTable dt, string sortString)
{
dt.DefaultView.Sort = sortString;
dt = dt.DefaultView.ToTable(); // at this point, when I view the
// DataTable dt, the rows are sorted correctly
}
在方法执行期间,查看"dt"DataTable显示正确排序的行。方法执行后,查看scheduleData
DataTable会显示未排序的行。
这里我不明白什么?
我认为也许它与方法中的DataTable ("dt")与scheduleData
一个有关。也许我正在排序"dt",但它实际上并没有影响我传递给方法的scheduleData
表(所以我应该使用一个不返回void的方法?)。
scheduleData.Columns.RemoveAt(0);
行:
private void loadData()
{
DataTable scheduleData = new DataTable();
/*
code that fills the DataTable with data and creates the groupOrderBy string
*/
deleteColumn(scheduleData);
}
private void deleteColumn(DataTable dt)
{
dt.Columns.RemoveAt(0);
}
这个简单的方法可以工作,当调用它后代码执行恢复时,scheduleData
数据表已经删除了它的列[0]。
显然,我对c#/编程相当陌生,所以我可能只是错过了一些非常简单的东西。我不明白为什么一个方法,操纵我的数据表工作(删除一列),但另一个方法,操纵数据表不工作(排序)。
在您的sortDT
方法中,您将dt
变量重新分配给dt.DefaultView.ToTable()
的结果,因此对dt
的引用在方法中发生了变化。引用的变化不会在方法外部反映出来。你基本上有两个选择:
- 从
sortDT
返回dt.DefaultView.ToTable()
的结果。(在这种情况下,我将调用方法GetSortedDataTable
。 - 传递
dt
作为方法的引用,如MSDN文档中所述。
我更喜欢第一个选项
问题是您传递了一个引用变量,但在方法中重置了引用。解决此问题的最快方法是使用ref关键字。
private void loadData()
{
DataTable scheduleData = new DataTable();
sortDT(ref scheduleData, "SortDate, Finish")
scheduleData.Columns.RemoveAt(0);
}
private void sortDT(ref DataTable dt, string sortString)
{
dt.DefaultView.Sort = sortString;
dt = dt.DefaultView.ToTable();
}
你也可以让SortDt返回数据表
private void loadData()
{
DataTable scheduleData = sortDT(scheduleData, "SortDate, Finish");
scheduleData.Columns.RemoveAt(0);
}
private DataTable sortDT(DataTable dt, string sortString)
{
dt.DefaultView.Sort = sortString;
return dt.DefaultView.ToTable();
}
你有两个选择要么使用ref要么返回DataTable
private void sortDT(ref DataTable dt, string sortString)
{
dt.DefaultView.Sort = sortString;
dt = dt.DefaultView.ToTable(); // at this point, when I view the
// DataTable dt, the rows are sorted correctly
}
private DataTable sortDT(DataTable dt, string sortString)
{
dt.DefaultView.Sort = sortString;
return dt.DefaultView.ToTable();
}