...

View Full Version : How to order by date



verbality
08-11-2011, 04:48 PM
Hello,

I am not a developer or programmer. I was asked to change some code so that events on an event page would load by date order. The code is below. What would I add/change? Thanks in advance!

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Linq;

public partial class Events : System.Web.UI.Page
{
#region Data

public struct DisplayItem
{
private string _EventDate;
public string _EventName;
public long _EventId;

public string EventDate
{
get { return _EventDate; }
set { _EventDate = value; }
}

public string EventName
{
get { return _EventName; }
set { _EventName = value; }
}

public long EventId
{
get { return _EventId; }
set { _EventId = value; }
}
}

#endregion

protected void Page_Load(object sender, EventArgs e)
{
string pageTitle = string.Empty;
long folderId = 0;
long CalendarID = 0;
string CalendarIdString = string.Empty;
bool showEvent;

//Get the CalendarID from the Query String
if (Request.QueryString.HasKeys())
{
if (!string.IsNullOrEmpty(Request.QueryString["CalendarId"]))
{
CalendarIdString = Request.QueryString["CalendarId"];
long.TryParse(CalendarIdString, out CalendarID);
}
}

//Seminars
if (CalendarID == 164)
{
pageTitle = "Free Seminars";
folderId = 132;
showEvent = false;
}
else //default
{
CalendarID = 172;
pageTitle = "Events";
folderId = 138;
showEvent = true;
}

CalendarID = 172;

MFCU.MFCU_Utilities.SetBreadCrumb(this, folderId);
this.litTitle.Text = pageTitle;
this.Title = pageTitle;

if (!this.IsPostBack)
{
this.LoadData(showEvent, CalendarID);
}
}

private void LoadData(bool showEvent, long calendarID)
{
List<DisplayItem> displayList = new List<DisplayItem>();
Ektron.Cms.Framework.Calendar.WebEvent calendarEvent = new Ektron.Cms.Framework.Calendar.WebEvent();
List<Ektron.Cms.Common.Calendar.WebEventData> listData;
listData = calendarEvent.GetEventOccurrenceList(calendarID, DateTime.Now, DateTime.Now.AddDays(60));
Ektron.Cms.API.Metadata mApi = new Ektron.Cms.API.Metadata();


listData.Reverse();

foreach (Ektron.Cms.Common.Calendar.WebEventData item in listData)
{
string freeSeminar = mApi.GetContentMetadataList(item.Id).GetItemByName("FreeSeminar").Value.ToString();
bool isFreeSeminar;

//Show Free seminars
if (!showEvent && freeSeminar.ToLower() == "no")
{
continue;
}

//Show events
if (showEvent && freeSeminar.ToLower() == "yes")
{
//continue;
}

DisplayItem dataItem = new DisplayItem();
dataItem.EventDate = item.EventStart.ToString("MMMM d h:mm tt");
dataItem.EventName = item.DisplayTitle;
dataItem.EventId = item.Id;
displayList.Add(dataItem);
}

this.Repeater1.DataSource = displayList;
this.Repeater1.DataBind();
}

}

dotnetmind
08-21-2011, 01:00 PM
It involves implementing the icomparer class.

Take this sample as Example:


Public Class DataGridViewSortRowsByDateTime
Implements System.Collections.IComparer
Private Direction As Integer = 1
Public Sub New(ByVal so As SortOrder)
If so = SortOrder.Descending Then
Direction = -1
ElseIf so = SortOrder.Ascending Then
Direction = 1
End If
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
Implements System.Collections.IComparer.Compare
Dim RowOne As DataGridViewRow = CType(x, DataGridViewRow)
Dim RowTwo As DataGridViewRow = CType(y, DataGridViewRow)
Dim Result As Integer = _
System.DateTime.Compare(RowOne.Cells(0).Value, RowTwo.Cells(0).Value)
Return Result * Direction
End Function
End Class

For Ascending Sort use this line:


DataGridView1.Sort(New DataGridViewSortRowsByDateTime(SortOrder.Ascending))

For Descending Sort use this line:


DataGridView1.Sort(New DataGridViewSortRowsByDateTime(SortOrder.Descending))



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum