I have a repeater control binded to a generic List<>.
The repeaters’ items are all in its own <div> tag, so it gives a good block visual effect, as well as a standard placeholder for all the items.
With custom sorting, I have been able to arrange the results among its own standard, but now I need something that isn’t standard.
I need to be able to take a selected item in a <div> block, and move it either to the bottom or the top of the repeaters’ item list.
Is there any way I am able to do this? Either by binding, or using the repeaters’ repeater_ItemDataBound() method?
The code is quite long.. So I will post what I think would be the ‘need to know’.
Populating the generic list
while (rdr.Read())
{
challenge.Add(new ChallengeList
{
GameName = rdr["gameName"].ToString(),
CreatorName = rdr["creatorName"].ToString(),
MediatorName = rdr["mediatorName"].ToString(),
ChallengeID = rdr["challengeId"].ToString(),
ChallengeAccepted = rdr["accepted"].ToString(),
MatchDate = DateTime.Parse(rdr["matchDate"].ToString()).ToString("dd MMMM yyyy hh:mm")
});
}
-Same Method- Sorting the data, then binding
switch (sort)
{
case "name":
Comparison<ChallengeList> name = ChallengeList.CompareGameName;
challenge.Sort(name);
break;
case "date":
Comparison<ChallengeList> date = ChallengeList.CompareDate;
challenge.Sort(date);
break;
case "status":
Comparison<ChallengeList> status = ChallengeList.CompareStatus;
challenge.Sort(status);
break;
}
rptChallenges.DataSource = challenge;
rptChallenges.DataBind();
Generic List Class (With sorting)
/// <summary>
/// Class ChallengeList
/// With sorting
/// </summary>
public class ChallengeList
{
/// <summary>
/// Compares the name of the game.
/// </summary>
/// <param name="game1">The game1.</param>
/// <param name="game2">The game2.</param>
/// <returns>System.Int32.</returns>
public static int CompareGameName(ChallengeList game1, ChallengeList game2)
{
return String.Compare(game1.GameName, game2.GameName, StringComparison.Ordinal);
}
/// <summary>
/// Compares the status.
/// </summary>
/// <param name="status1">The status1.</param>
/// <param name="status2">The status2.</param>
/// <returns>System.Int32.</returns>
public static int CompareStatus(ChallengeList status1, ChallengeList status2)
{
return string.Compare(status1.ChallengeAccepted, status2.ChallengeAccepted, StringComparison.Ordinal);
}
/// <summary>
/// Compares the date.
/// </summary>
/// <param name="date1">The date1.</param>
/// <param name="date2">The date2.</param>
/// <returns>System.Int32.</returns>
public static int CompareDate(ChallengeList date1, ChallengeList date2)
{
return string.Compare(date1.MatchDate, date2.MatchDate, StringComparison.Ordinal);
}
/// <summary>
/// Compares the date reverse.
/// </summary>
/// <param name="date2">The date2.</param>
/// <param name="date1">The date1.</param>
/// <returns>System.Int32.</returns>
public static int CompareDateReverse(ChallengeList date2, ChallengeList date1)
{
return string.Compare(date1.MatchDate, date2.MatchDate, StringComparison.Ordinal);
}
/// <summary>
/// Gets or sets the name of the game.
/// </summary>
/// <value>The name of the game.</value>
public string GameName { get; set; }
/// <summary>
/// Gets or sets the name of the creator.
/// </summary>
/// <value>The name of the creator.</value>
public string CreatorName { get; set; }
/// <summary>
/// Gets or sets the name of the mediator.
/// </summary>
/// <value>The name of the mediator.</value>
public string MediatorName { get; set; }
/// <summary>
/// Gets or sets the challenge ID.
/// </summary>
/// <value>The challenge ID.</value>
public string ChallengeID { get; set; }
/// <summary>
/// Gets or sets the challenge accepted.
/// </summary>
/// <value>The challenge accepted.</value>
public string ChallengeAccepted { get; set; }
/// <summary>
/// Gets or sets the match date.
/// </summary>
/// <value>The match date.</value>
public string MatchDate { get; set; }
}
Real-world application of my question
If there is a ‘stickied’ match – it must appear on top of the matches regardless of date, status or name
I would recommend that you order the data in your list before you see ditto the repeater. If this is not possible given structure of your data and/or the item template of the repeater, could you please include samples of the data and the repeater so that we may help further?
EDIT after asker posted code
rather than setting challenge as the data source for the repeater you should separate all the items that need to be on the top of the list into a new list and then append the other other items to the end of the top items.
something like this:
depending on what the criteria is for an item to be on the top, you may be able to use a LINQ expression to simplify this code.