-1

I have a User Control which have multiple labels containing values, such as a date for Uploaded, a string for Title, a string for Author, ect. As of right now I'm populating the stackpanel by creating a new instance of the user control for each row of data in the SQL database.

What I would like is to click on a "Sort by Date" button and have all the instances of the User Control object sort by the Uploaded Date in the Stackpanel.

How would I procced going with this?

Some of the code behind populating the stackpanel with new instances of the User Control.

/// <summary>
/// Receive data from SQL and apply them to a new instance of itemControl
/// </summary>
public static void GetResultItems(StackPanel stackPanel)
{
    // The SQL Query
    string Query = "select * from Items";
    MySqlCommand command = new MySqlCommand(Query, connection);

    // Open connection
    connection.Open();
    MySqlDataReader reader = command.ExecuteReader();

    // While reading...
    while (reader.Read())
    {
    // Creates new instance of the itemControl
    ItemControl itemControl = new ItemControl();

    // Assign the data from the SQL database onto the itemControl labels
    //Upload Date. This contains the date for when it was Uploaded. 
    itemControl.lblListUploaded.Content = (reader["UploadDate"].ToString()); //I wish to use this value to sort by

    //Author
    itemControl.lblExpandAuthor.Content = "Author: " + (reader["Author"].ToString());

    // Add the new instance of ItemControl to the resultItem List<>
    //The list is never used, but is created incase of later usage/need.
    resultItem.Add(itemControl);

    // Add the item to the Stackpanel
    stackPanel.Children.Add(itemControl);
    }

    // Close connection
    connection.Close();
    }
}

This is a photo of how the Stackpanel looks

Noam M
  • 3,156
  • 5
  • 26
  • 41
Trinax
  • 31
  • 3
  • Your question is way too broad. One obvious solution is of course to have the data returned in sorted order, by modifying the query. If you can't do that, then the usual WPF approach is to apply a `CollectionViewSource` to the source data and set the sort options in that. However, that works best if you don't start with broken WPF code, as you have here. I.e. you should not be manipulating UI objects directly; instead, bind your collection and use a template with more data binding to control the visual presentation. – Peter Duniho Jul 20 '17 at 05:37

1 Answers1

0

How about

string Query = "select * from Items orderby UploadDate";
Sushil Mate
  • 583
  • 6
  • 14