我创建了2行代码来对DataTable进行排序。我需要多次执行此操作,因此我将这两行移动到一个简单的方法中,每次需要进行排序时都会调用它。
但是,当我调用该方法以对DataTable进行排序时,DataTable不会维护排序。这里肯定会发生一些我不知道的事情,我不明白。
我的2行符合我的要求:
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会显示未排序的行。
我在这里不理解什么?
我认为这可能与方法(“dt”)中的DataTable和“scheduleData”中的DataTable有关。也许我正在排序“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”DataTable已删除了Column [0]。
显然,我对C#/编程很新,所以我可能只是错过了一些非常简单的东西。我不明白为什么一个操纵我的DataTable的方法工作(删除一个列),但另一个操作DataTable的方法不起作用(排序)。
答案 0 :(得分:3)
在sortDT
方法中,您将dt
变量重新分配给dt.DefaultView.ToTable()
的结果,因此对dt
内部的引用进行了更改。参考的变化不会反映在方法之外。所以,你基本上有两个选择:
dt.DefaultView.ToTable()
返回sortDT
的结果。 (在这种情况下,我会调用方法GetSortedDataTable
。dt
作为方法的参考,如here in the MSDN docs所述。我更喜欢第一种选择。
答案 1 :(得分:2)
问题是您正在传递引用变量,但重置方法中的引用。解决此问题的最快方法是使用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返回DataTable。
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();
}
答案 2 :(得分:1)
您有两个选项:使用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();
}