none
Dataview.Sort not working. RRS feed

  • Question

  • This is weird.

    I have a DataView that I create from a DataTable like this:

    -------------------------------------------------------

    DataView masterDv = new DataView(masterTable);

    -------------------------------------------------------

    Column #12 is called 'CLIENT':

    -------------------------------------------------------

    masterDv.Table.Columns[12].ColumnName="CLIENT"

    -------------------------------------------------------

    When I set the sort property of the DataView, the Dataview doesn't change.

    masterDv.Sort = "CLIENT";

    -------------------------------------------------------

    Am i missing something?

     

    thanks.

     

    doug

     

    Wednesday, August 9, 2006 4:22 PM

Answers

  • Hi,

     

    How are you accessing the DataView? from the code above seems that you're accessing the rows from DataView.Table.Rows[RowIndex] and that won't reflect your sorting, after applying sort you can access sorted rows like DataView[rowIndex] and will be able to see the sorted data.

     

    DataTable masterTable;

    DataView dv = new DataView(masterTable);
    dv.Sort = "PROJECT";

    //dvIdea is the 'i' sorted record

     

    I hope this can help you please let me know

     

    Best regards

    Juan Carlos Vazquez

     

    Thursday, September 25, 2008 12:01 AM
    Moderator

All replies

  • although the sort is suppose to default to ascending have you tried to explicitly state "ASC" and then "DESC" to see if that makes a diff?

    masterDv.Sort = "CLIENT ASC";

    masterDv.Sort = "CLIENT DESC";

     

    HTH

    Wednesday, August 9, 2006 5:41 PM
  • no.

    it makes no difference if I stipulate ASC or DESC

    I can't undersand it..

    thanks.

    Wednesday, August 9, 2006 6:17 PM
  • Is the DataView set as the default view on the DataTable?

    Something like: masterTable.DefaultView = masterDv;

    As far as viewing the sorted DataTable, either the DataView has to be the default view for the DataTable or the DataSource for the control you view the data through has to be masterDv...

    Hope this help? If not, could you describe how you are displaying the sorted data?

     

     

     

    Wednesday, August 9, 2006 7:51 PM
  • I cannot set the DefaultView property on the masterTable.  The compiler complains that the property is read only.

    Error 1 Property or indexer 'System.Data.DataTable.DefaultView' cannot be assigned to -- it is read only C:\Documents and Settings\DEXTERDR\My Documents\Visual Studio 2005\Projects\FocusDPE\FocusDPE\FocusDataReader.cs 187 13 FocusDPE

    This is crazy!

     

     

     

    Thursday, August 10, 2006 1:51 PM
  • No problem. I'm sorry I didn't notice that... apply to the sort string to the DataTable.DefaultView.Sort property.

    Let me know if that worked...

    Thursday, August 10, 2006 2:22 PM
  • nope..

    I'm  at a loss..

    I set up a console app just to test and the SAME EXACT functionality is working.

    I just don't get it....

    The context of the sort is within a Custom Data Extension for SQL Server Reporting Services

    Here's my code just in case there's something I missed.

    DataTable masterTable;

    bool Microsoft.ReportingServices.DataProcessing.IDataReader.Read(){
    DataView dv = new DataView(masterTable);
    dv.Sort = "PROJECT";
    int colCounter = 0;
    while (rowCounter < dv.Table.Rows.Count)
    {
     if (dv.Table.Rows != null)
     {

      foreach (DataColumn dc in dv.Table.Columns)

      {

       m_cols[colCounter] = dv.Table.Rows[rowCounter].ItemArray[colCounter].ToString();

       colCounter++;

      }

      rowCounter++;
      return true;
     }
    }
    return false;
    }

    Thanks in advance...

     

    Doug


    Thursday, August 10, 2006 8:22 PM
  • Did you every figure this out? I am facing the same issue.
    Friday, October 27, 2006 5:58 PM
  • Hi there,

    I think the problem you are encountering is due to you accessing the wrong "view"...

    Once you have run the sort function you have 2 "views" - the default view, which is accessed by dv.Table and the new (sorted) view... accessed by dv[0]

    Try that - you should find it sorts you out

    Cheers

    Mike

    Friday, November 10, 2006 11:55 AM
  •  MikeyK65 wrote:

    Hi there,

    I think the problem you are encountering is due to you accessing the wrong "view"...

    Once you have run the sort function you have 2 "views" - the default view, which is accessed by dv.Table and the new (sorted) view... accessed by dv[0]

    Try that - you should find it sorts you out

    Cheers

    Mike



    I have just tried your suggestion.
    But it still do not work, can you or any one please give me some help?

    'This is how I do the sorting.
       Dim oDataView As DataView = New DataView(dtReport)
       oDataView.Sort = "sReportType desc, sGender desc"

    'And loop though
      For i = 0 To oDataView(1).DataView.Table.Rows.Count - 1
    dosomething
      Next
    'Or

      For i = 0 To oDataView(1).DataView.Table.Rows.Count - 1
    dosomething
      Next


    Thank you very much


    Wednesday, July 2, 2008 5:18 AM
  • Hi,

     

    How are you accessing the DataView? from the code above seems that you're accessing the rows from DataView.Table.Rows[RowIndex] and that won't reflect your sorting, after applying sort you can access sorted rows like DataView[rowIndex] and will be able to see the sorted data.

     

    DataTable masterTable;

    DataView dv = new DataView(masterTable);
    dv.Sort = "PROJECT";

    //dvIdea is the 'i' sorted record

     

    I hope this can help you please let me know

     

    Best regards

    Juan Carlos Vazquez

     

    Thursday, September 25, 2008 12:01 AM
    Moderator
  • I know this is an old post, but I don't think that anyone adequately answered it.

     

    The problem is that if you have a DataView, you need to iterate through the view, not the table. So, you had this code:

     

    Code Snippet

    'This is how I do the sorting.
       Dim oDataView As DataView = New DataView(dtReport)
       oDataView.Sort = "sReportType desc, sGender desc"

    'And loop though
      For i = 0 To oDataView(1).DataView.Table.Rows.Count - 1

    dosomething

      Next

     

     

    But what you really needed to do is this:

    Code Snippet

     

    'This is how I do the sorting.
       Dim oDataView As DataView = New DataView(dtReport)
       oDataView.Sort = "sReportType desc, sGender desc"

    'And loop though
      For i = 0 To oDataView.Count - 1

    dosomething

      Next

     

     

    Somewhere in the above thread, someone also mentioned the DefaultView. If you were to use the DefaultView of your DataTable, you'd need code like this:

     

    Code Snippet

    'This is how I do the sorting.
       dtReport.DefaultView.Sort = "sReportType desc, sGender desc"

    'And loop though
      For i = 0 To dtReport.DefaultView.Count - 1

    dosomething

      Next

     

     

     

    Monday, October 6, 2008 1:45 AM

  • Have you tried

    dv[0].Row[0]
    dv[1].Row[0]
    dv[2].Row[0]
    .
    .
    .
    dv[n].Row[0]

    Which is the sorted datarowview.

    Thursday, April 16, 2009 3:22 PM
  • hi Juan Carlos Vazquez, thanks. It helped me a lot. and worked well.

    thanks and regards

    Venaktesh
    Tuesday, May 12, 2009 9:41 AM
  • Big thanks to BonnieB.  Clear and easy to follow explanation!
    Friday, January 13, 2012 5:13 PM
  • Use 

    masterTable.DefaultView.Sort = "CLIENT";

    instead of 

    masterDv.Sort = "CLIENT";

    Monday, January 23, 2012 8:41 AM
  • You're welcome, Gary. I'm glad my explanations made some kind of sense. =0)


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Edited by BonnieBMVP Tuesday, January 24, 2012 3:22 PM
    Tuesday, January 24, 2012 3:21 PM
  • @suleRi -- sometimes you need to create a new DataView and not use the DefaultView, so I'm not sure what the point is to your suggestion ...
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Tuesday, January 24, 2012 3:23 PM
  • It's works for my:

    DataView dv = myTable.DefaultView;

    dv.sort ="myField";

    myTable=dv.ToTable();

    Best regards.

    Aldo.

    Monday, June 24, 2013 2:00 AM