Zalmoxis Blog Rotating Header Image

Sorting Foreign Key Values in Dynamic Data Web Site

I wanted to sort the values in alphabetic order when choosing foreign key relationship in Insert/Edit Entity Mode.

And I have decided to sort the DropDownList control in ForeignKey_Edit.ascx file.

I added the following method:

#region SortDropDownList

/// <summary>

/// method to sort a DropDownList

/// </summary>

/// <param name="ddl">DropDownList to sort</param>

public void SortDropDownList(DropDownList ddl)

{

    //create a ListItem array the size of the items

    //in your DropDownList

    List<ListItem> sorted =new List<ListItem>();

    foreach (ListItem item in ddl.Items)

    {

        sorted.Add(item);

    }

    sorted = sorted.OrderBy(a => a.Text).ToList();

    

    //remove all items from the DropDownList

    ddl.Items.Clear();

    //add the sorted items to the DropDownList

    ddl.Items.AddRange(sorted.ToArray());

}

#endregion

And add the calling right after PopulateListControl method in the Page_Load event handler:

protected void Page_Load(object sender, EventArgs e)

    {

        if (DropDownList1.Items.Count == 0)

        {

            if (!Column.IsRequired)

            {

                DropDownList1.Items.Add(new ListItem("[Not Set]", ""));

            }

            PopulateListControl(DropDownList1);

 

            //sorting DropDown by Text 

            SortDropDownList(DropDownList1);

        }

    }

And that’s it.

Popularity: 2% [?]

Post to Delicious Post to Digg Post to Facebook

Related posts:

  1. Ways to Visualize Data A Periodic Table of Visualization Methods http://www.visual-literacy.org/periodic_table/periodic_table.html...
  2. Great Excavator Videos For My Son A collection of several Excavator videos from Youtube. My sone...
  3. SqlBulkCopy – Fast Way For Importing CSV Files to SQL Server If you need to import large CSV files in SQL...
  4. oceans twelve laser dance: This is my current ringtone: oceans twelve laser dance:Enjoy...

Leave a Reply