none
Sorting in Data Set C# RRS feed

  • Question

  • I am new to Dot Net Programming.

    I have a DataSet with 8 tables, 2nd Table is Child to 1st Table, 3rd Table is Child to 2nd and So on. In 5th Table there is a Column Amount, I have achieved the sorting in the following way in DS.

    ds.Tables["MyTable"].DefaultView.Sort = "Amount ASC";

    But in its parent tables the data is not getting aligned according to this table. Please advice how to sort data in its parent tables according to this table. I have loaded this data from a XML Webservice.

    Thanks in Advance...

    Tuesday, May 8, 2012 9:58 AM

Answers

  • Sam,

    A dataset is an partial representation of the data which has to be handled in a database. It follows the rules of relational databases. 

    Therefore the dataset is in fact the database, while it has also collections for datatables and relations. 

    If we are talking about sorting then that can only be for representation (a view). 

    If you want to use parent tables, then use relations. In my perception is the best thing to include those in the Database itself. In that way you can use the TableAdapterManager which makes properties so you can simply tell in your table to use the parent item.

    If you don't set the relations in in your database you will see that this old approach does go only if you have one or two tables in your database. In that case you can set the relations also in code.


    Success
    Cor


    Tuesday, May 8, 2012 8:35 AM
  • Hi Sam,

    You can use DataView Manager, please refer this link here: http://msdn.microsoft.com/en-us/library/aa984340(v=vs.71).aspx

    ==============

    A data view manager is particularly useful when you are working with related tables and want to sort or filter child records from a master table. 

    http://msdn.microsoft.com/en-us/library/b51xae2y(v=vs.71).aspx

    ==============

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 9, 2012 6:49 AM
    Moderator

All replies

  • I am new to Dot Net Programming.

    I have a DataSet with 8 tables, 2nd Table is Child to 1st Table, 3rd Table is Child to 2nd and So on. In 5th Table there is a Column Amount, I have achieved the sorting in the following way in DS.

    ds.Tables["MyTable"].DefaultView.Sort = "Amount ASC";

    But in its parent tables the data is not getting aligned according to this table. Please advice how to sort data in its parent tables according to this table. I have loaded this data from a XML Webservice.

    Thanks in Advance...

    Tuesday, May 8, 2012 5:36 AM
  • Sam,

    A dataset is an partial representation of the data which has to be handled in a database. It follows the rules of relational databases. 

    Therefore the dataset is in fact the database, while it has also collections for datatables and relations. 

    If we are talking about sorting then that can only be for representation (a view). 

    If you want to use parent tables, then use relations. In my perception is the best thing to include those in the Database itself. In that way you can use the TableAdapterManager which makes properties so you can simply tell in your table to use the parent item.

    If you don't set the relations in in your database you will see that this old approach does go only if you have one or two tables in your database. In that case you can set the relations also in code.


    Success
    Cor


    Tuesday, May 8, 2012 8:35 AM
  • Hi Sam,

    You can use DataView Manager, please refer this link here: http://msdn.microsoft.com/en-us/library/aa984340(v=vs.71).aspx

    ==============

    A data view manager is particularly useful when you are working with related tables and want to sort or filter child records from a master table. 

    http://msdn.microsoft.com/en-us/library/b51xae2y(v=vs.71).aspx

    ==============

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 9, 2012 6:49 AM
    Moderator
  • Data sorting information is contained in grid headers. Users may set single or multiple sorting in one or multiple headers. On programming level, sorting

    looks as follows:

    //Get top-level header
    Header header = grid.Headers[0];

    header["Product"].SortDirection = SortDirection.Ascending;
    header["Price"].SortDirection = SortDirection.Descending;

    Example of sorting by date represented with DateTime type. I think you are satisfied

    class DateExample
    {
        private readonly DateTime _date;

        public DateExample(DateTime date)
        {
            _date = date;
        }

        public DateTime Date
        {
            get { return _date; }
        }
    }

    //Create header and column
    grid.Headers.Add(new Header());
    grid.Headers[0].Add(new Column("Date"));

    //Configure the header
    grid.Headers[0].StretchMode = ColumnStretchMode.All;
    grid.Headers[0][0].SortDirection = SortDirection.Ascending;
    grid.Headers[0]["Date"].Format = new StringFormat("d", new CultureInfo("fr-FR"));

    //Populate grid with random data
    Random random = new Random();
    BindingList<DateExample> source = new BindingList<DateExample>();
    for (int i = 0; i < 6; ++i)
    {
        source.Add(new DateExample(DateTime.Now + TimeSpan.FromDays(random.Next(1000))));
    }
    grid.DataSource = source;
    dapfor. com

    Monday, November 19, 2012 7:47 AM