none
How to Display in Sorted Order using Linq and XML RRS feed

  • Question

  • Hi Every Body
    I am creating a web site now I stuck at a point where I want to display the result in sorted order. Actually I have a admin login from where I can add edit delete schedule for class I am conducting.

    Say for example I have this existing schedule -
    Class 1 Monday 04:00PM
    Class 2 Tuesday 12:30 PM
    Class 3 Wednesday 10:00 AM

    Now if i add -
    Class 4 Monday 12:30 PM and
    Class 5 Thrusday 03:00 PM

    then these classes should be displayed in previous list schedule but not at the end of previous list, it should looks like -
    Class 4 Monday 12:30 PM and
    Class 1 Monday 04:00PM
    Class 2 Tuesday 12:30 PM
    Class 3 Wednesday 10:00 AM
    Class 5 Thrusday 03:00 PM

    I hope I make myself clear to you all.

    Please see the attached code for the reference
    to see my work done visit
    http://www.lovepilatesleeds.com/mat-classes.aspx 

    using System;   
    using System.Data;   
    using System.Collections;   
    using System.Collections.Generic;   
    using System.Linq;   
    using System.Web;   
    using System.Web.UI;   
    using System.Web.UI.WebControls;   
      
    public partial class admin_manage_time_table : System.Web.UI.Page   
    {   
        public DataSet ClassGuides   
        {   
            get  
            {   
                DataSet dsClassGuides = new DataSet("ClassGuides");   
                dsClassGuides.ReadXmlSchema(Server.MapPath("~/admin/xml/ClassGuide.xml"));   
                dsClassGuides.ReadXml(Server.MapPath("~/admin/xml/ClassGuide.xml"));   
                return dsClassGuides;   
            }   
        }   
        public DataSet TimeTables   
        {   
            get  
            {   
                DataSet dsTimeTables = new DataSet("TimeTables");   
                dsTimeTables.ReadXmlSchema(Server.MapPath("~/admin/xml/TimeTableSchema.xml"));   
                dsTimeTables.ReadXml(Server.MapPath("~/admin/xml/TimeTable.xml"));   
                return dsTimeTables;   
            }   
        }   
        public string CurrentRowID   
        {   
            get { return Convert.ToString(ViewState["CurrentRowID"]); }   
            set { ViewState["CurrentRowID"] = value; }   
        }   
        public DataRow getTimeTableById(string ID)   
        {   
            foreach (DataRow dr in TimeTables.Tables[0].Rows)   
            {   
                if (dr["ID"].ToString() == ID) return dr;   
            }   
            return null;   
        }   
        public string[] GetSelectedIDs()   
        {   
            string ids = "";   
            foreach (RepeaterItem item in rptTimeTable.Items)   
            {   
                if ((item.FindControl("chkSelect") as CheckBox).Checked) ids += (item.FindControl("hidID") as HiddenField).Value + ",";   
            }   
            return ids.TrimEnd(',').Split(',');   
        }   
        protected void Page_Load(object sender, EventArgs e)   
        {   
            this.MaintainScrollPositionOnPostBack = true;   
            if (!IsPostBack)   
                LoadTimeTables();   
        }   
      
        private void LoadTimeTables()   
        {   
            rptTimeTable.DataSource = TimeTables;   
            rptTimeTable.DataBind();   
            ddlFromHour.Items.Add(new ListItem("--", ""));   
            ddlFromMinute.Items.Add(new ListItem("--", ""));   
            ddlToHour.Items.Add(new ListItem("--", ""));   
            ddlToMinute.Items.Add(new ListItem("--", ""));   
            for (int i = 1; i < 13; i++)   
            {   
                ddlFromHour.Items.Add(new ListItem(i.ToString(), i.ToString()));   
                ddlToHour.Items.Add(new ListItem(i.ToString(), i.ToString()));   
            }   
            for (int i = 1; i < 60; i++)   
            {   
                ddlFromMinute.Items.Add(new ListItem(i.ToString(), i.ToString()));   
                ddlToMinute.Items.Add(new ListItem(i.ToString(), i.ToString()));   
            }   
        }   
        protected void rptTimeTable_ItemCommand(object source, RepeaterCommandEventArgs e)   
        {   
            if (e.CommandName == "EditTimeTable")   
            {   
                ddlClass.Items.Clear();   
                ddlClass.AppendDataBoundItems = true;   
                ddlClass.Items.Add(new ListItem("Select one...........", ""));   
                ddlClass.DataTextField = "ClassName";   
                ddlClass.DataValueField = "ClassName";   
                ddlClass.DataSource = ClassGuides;   
                ddlClass.DataBind();   
                btnSave.Text = "Save";   
                ViewState["Mode"] = "Edit";   
                traddedit.Visible = true;   
                headerspan.InnerHtml = "Add/Edit Timetable";   
                DataRow dr = getTimeTableById(e.CommandArgument.ToString());   
                this.CurrentRowID = e.CommandArgument.ToString();   
                ddlDay.SelectedValue = dr["Day"].ToString();   
                txtLocation.Text = dr["Location"].ToString();   
                txtVenue.Text = dr["Venue"].ToString();   
                ddlFromHour.SelectedValue = dr["FromHour"].ToString();   
                ddlFromMinute.SelectedValue = dr["FromMinute"].ToString();   
                ddlFromAMPM.SelectedValue = dr["FromAMPM"].ToString();   
                ddlToHour.SelectedValue = dr["ToHour"].ToString();   
                ddlToMinute.SelectedValue = dr["ToMinute"].ToString();   
                ddlToAMPM.SelectedValue = dr["ToAMPM"].ToString();   
                   
                try  
                {   
                    ddlClass.SelectedValue = dr["Class"].ToString();   
                }   
                catch { ddlClass.SelectedValue = ""; }   
                //txtClassGuide.Text = dr["ClassGuide"].ToString();   
                ddlLevel.SelectedValue = dr["Level"].ToString();   
                txtOtherLevel.Text = dr["LevelOther"].ToString();   
                txtOtherLevel.Visible = ddlLevel.SelectedValue == "Other";   
                ddlDates.SelectedValue = dr["Dates"].ToString();   
                txtOtherDates.Text = dr["DatesOther"].ToString();   
                txtOtherDates.Visible = ddlDates.SelectedValue == "Other";   
                txtPrice.Text = dr["Price"].ToString();   
      
      
            }   
        }   
        protected void ddlLevel_SelectedIndexChanged(object sender, EventArgs e)   
        {   
            txtOtherLevel.Visible = ddlLevel.SelectedValue == "Other";   
            txtOtherLevel.Text = "";   
        }   
        protected void ddlDates_SelectedIndexChanged(object sender, EventArgs e)   
        {   
            txtOtherDates.Visible = ddlDates.SelectedValue == "Other";   
            txtOtherDates.Text = "";   
        }   
        void SetControlValueToDataRow(DataRow dr)   
        {   
            dr["Day"] = ddlDay.SelectedValue;   
            dr["Location"] = txtLocation.Text;   
            dr["Venue"] = txtVenue.Text;   
            dr["FromHour"] = ddlFromHour.SelectedValue;   
            dr["FromMinute"] = ddlFromMinute.SelectedValue;   
            dr["FromAMPM"] = ddlFromAMPM.SelectedValue;   
            dr["ToHour"] = ddlToHour.SelectedValue;   
            dr["ToMinute"] = ddlToMinute.SelectedValue;   
            dr["ToAMPM"] = ddlToAMPM.SelectedValue;   
               
            dr["Class"] = ddlClass.SelectedValue;   
            //dr["ClassGuide"] = txtClassGuide.Text;   
            dr["Level"] = ddlLevel.SelectedValue;   
            dr["LevelOther"] = ddlLevel.SelectedValue == "Other" ? txtOtherLevel.Text : "";   
            dr["Dates"] = ddlDates.SelectedValue;   
            dr["DatesOther"] = txtOtherDates.Text;   
            dr["DatesOther"] = ddlDates.SelectedValue == "Other" ? txtOtherDates.Text : "";   
            dr["Price"] = txtPrice.Text;   
        }   
        protected void btnSave_Click(object sender, EventArgs e)   
        {   
            DataSet ds = TimeTables;   
            DataRow dr = null;   
            if (ViewState["Mode"].ToString() == "Edit")   
            {   
                foreach (DataRow dr1 in ds.Tables[0].Rows)   
                {   
                    if (dr1["ID"].ToString() == this.CurrentRowID) dr = dr1;   
                }   
            }   
            else  
            {   
                dr = ds.Tables[0].NewRow();   
                try  
                {   
                    dr["ID"] = (Convert.ToInt32(TimeTables.Tables[0].Rows[TimeTables.Tables[0].Rows.Count - 1]["ID"]) + 1).ToString();   
                }   
                catch { dr["ID"] = "1"; }   
            }   
            SetControlValueToDataRow(dr);   
            if (ViewState["Mode"].ToString() == "Add")   
                ds.Tables[0].Rows.Add(dr);   
            dr.AcceptChanges();   
            ds.AcceptChanges();   
            ds.WriteXml(Server.MapPath("~/admin/xml/TimeTable.xml"));   
            rptTimeTable.DataSource = TimeTables;   
            rptTimeTable.DataBind();   
            traddedit.Visible = false;   
        }   
        protected void btnDelete_Click(object sender, EventArgs e)   
        {   
            string[] ids = GetSelectedIDs();   
            DataSet ds = TimeTables;   
            ArrayList arrDataRows = new ArrayList();   
            foreach (DataRow dr in ds.Tables[0].Rows)   
            {   
                foreach (string id in ids)   
                {   
                    if (id == dr["ID"].ToString())   
                        arrDataRows.Add(dr);   
                }   
            }   
            foreach (DataRow dr in arrDataRows)   
                ds.Tables[0].Rows.Remove(dr);   
            ds.AcceptChanges();   
            ds.WriteXml(Server.MapPath("~/admin/xml/TimeTable.xml"));   
            rptTimeTable.DataSource = TimeTables;   
            rptTimeTable.DataBind();   
      
        }   
        protected void btnCancel_Click(object sender, EventArgs e)   
        {   
            traddedit.Visible = false;   
        }   
        protected void btnAdd_Click(object sender, EventArgs e)   
        {   
            ddlDay.SelectedValue = "";   
            txtLocation.Text = "";   
            txtVenue.Text = "";   
            ddlFromHour.SelectedValue = "";   
            ddlFromMinute.SelectedValue = "";   
            ddlFromAMPM.SelectedValue = "0";   
            ddlToHour.SelectedValue = "";   
            ddlToMinute.SelectedValue = "";   
            ddlToAMPM.SelectedValue = "0";   
               
            ddlClass.SelectedIndex = 0;   
            //txtClassGuide.Text = "";   
            ddlLevel.SelectedValue = "";   
            txtOtherLevel.Text = "";   
               
            ddlDates.SelectedValue = "";   
            txtOtherDates.Text = "";   
               
            txtPrice.Text = "";   
            ViewState["Mode"] = "Add";   
            traddedit.Visible = true;   
        }   
    }  
    And here is the XML code -<br/><pre lang=x-xml><?xml version="1.0" standalone="yes"?>  
    <TimeTables>  
      <TimeTable>  
        <ID>2</ID>  
        <Day>Monday</Day>  
        <Location>Headingley</Location>  
        <Venue>The Yorkshire College of Music, Headingley, LS6 2DD</Venue>  
        <FromHour>11</FromHour>  
        <FromMinute />  
        <FromAMPM>0</FromAMPM>  
        <ToHour>12</ToHour>  
        <ToMinute />  
        <ToAMPM>1</ToAMPM>  
        <Class>Pilates Mat Work</Class>  
        <Level>Other</Level>  
        <LevelOther>Beginner</LevelOther>  
        <Dates>Other</Dates>  
        <DatesOther>11th Jan-15th Mar</DatesOther>  
        <Price>59 per 10 wk course</Price>  
      </TimeTable>
    </TimeTables>  
    
    
    Please help me and guide me with some code which can give my desired out put
    Thanking you all
    Regards
    • Moved by Vitek Karas - MSFTMicrosoft employee Monday, January 4, 2010 9:46 AM Your code uses DataSet to handle the data, so this forum is more appropriate for this question. (From:XML and the .NET Framework)
    Monday, January 4, 2010 6:51 AM

Answers

  • Hi,

    You can use DataTable.DefaultView.Sort to get values sorted, but you will need to change your current data structure to use enumeration for Day and 24h time format instead of AM/PM. Another option is to create a list of rows and sort those rows using a comparer or delegate.

    List<DataRow> rows = new List<DataRow>(dataTable.Select());
    rows.Sort(delegate(DataRow dr1, DataRow dr2) 
    { 
        // compare rows
    });

    Regards,
    Danijel

    Blog, Twitter
    Monday, January 4, 2010 11:42 AM