elmer007
elmer007

Reputation: 1445

DataTable Sorting Lost When Done Via Method

I created 2 lines of code to sort my DataTable. I needed to do this several times, so I then moved the 2 lines into a simple method that I would call each time I needed to sort.

However, when I call the method in order to sort my DataTable, the DataTable does not maintain the sorting. There must be something taking place here that I am not aware of and that I do not understand.

My 2 lines that work as I want them to:

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
}

When I view the scheduleData DataTable after the above lines run, it looks like how I want it (the rows are in the correct order).

So, I tried to move them into a method as shown below:

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
}

During the method's execution, viewing the "dt" DataTable shows correctly sorted rows. After the method's execution, viewing the scheduleData DataTable shows rows that are not sorted.

What am I not understanding here?

I thought that maybe it had to do with the DataTable in the method ("dt") versus the scheduleData one. Perhaps I was sorting "dt", but it wasn't actually affecting the scheduleData table that I passed into the method (so I should be using a method that doesn't return void?).

So, I tried this instead of the scheduleData.Columns.RemoveAt(0); line above:

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

That simple method works, and when the code execution resumes after calling it, the scheduleData DataTable has had its Column[0] removed.

Obviously, I'm rather new to C#/programming, so I may just be missing something very simple. I don't understand why one method that manipulates my DataTable works (the removing of a column), but another method that manipulates the DataTable does not work (the sorting).

Upvotes: 0

Views: 567

Answers (3)

Vivek Nuna
Vivek Nuna

Reputation: 1

You have two options either use ref or return 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();  
}

Upvotes: 1

Paul Tsai
Paul Tsai

Reputation: 903

The problem is that you are passing a reference variable, but resetting the reference in the method. The quickest way to resolve this issue is to use the ref keyword.

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

You could also have SortDt return the 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();  
}

Upvotes: 2

M.E.
M.E.

Reputation: 2929

In your sortDT method, you are reassiging the dt variable to the result of dt.DefaultView.ToTable(), thus the reference to dt changes within the method. The change of the reference doesn't get reflected outside the method. So, you basically have two options:

  1. Return the result of dt.DefaultView.ToTable() from your sortDT. (In that case I would call the method GetSortedDataTable.
  2. Pass dt as reference to the method, as stated here in the MSDN docs.

I'd prefer the first option.

Upvotes: 3

Related Questions