iterate a sharepoint list - c#

In code how can I access a list e.g "MyList" in sharepoint,
then iterate through this list items and get the value of a particular column on that list e.g the "URL" column?

To retrieve all items from a list and iterate through each one, the best solution would be as follows (assuming that this code is run as part of a feature):
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
using(SPSite site = properties.Feature.Parent as SPSite)
{
SPList list = site.RootWeb.Lists["ListName"];
SPListItemCollection items = list.Items;
foreach (SPListItem listItem in items)
{
Response.Write(SPEncode.HtmlEncode(listItem["Url"].ToString()) +"<BR>");
}
}
}
But if the list is very large, it would be better to paginate through the list items:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
using(SPSite site = properties.Feature.Parent as SPSite)
{
SPList list = site.RootWeb.Lists["ListName"];
if(items.ItemCount > 100)
{
SPQuery query = new SPQuery();
query.RowLimit = 100;
int index = 1;
do
{
SPListItemCollection items = list.GetItems(query);
foreach (SPListItem listItem in items)
{
Response.Write(SPEncode.HtmlEncode(listItem["Url"].ToString()) +"<BR>");
}
query.ListItemCollectionPosition = items.ListItemCollectionPosition;
index++;
} while (query.ListItemCollectionPosition != null);
}
else
{
SPListItemCollection items = list.Items;
foreach (SPListItem listItem in items)
{
Response.Write(SPEncode.HtmlEncode(listItem["Url"].ToString()) +"<BR>");
}
}
}
}
This is based on the Microsoft's Best Practices for SharePoint.

From this blog post:
The correct way to do it is to store the Items property return value in a SPListItemCollection variable. With this the database is only queried once and we will then iterate over the result set that is stored within the collection object. Here is the changed sample code:
SPListItemCollection items = SPContext.Current.List.Items;
for(int i=0;i<100 && i<items.Count;i++) {
SPListItem listItem = items[i];
htmlWriter.Write(listItem["Title"]);
}

You can also iterate the items directly, and if you're using a URL field you probably want to use the SPFieldUrlValue class, so you don't have to deal with the way SharePoint stores URLs:
foreach(SPListItem item in spList.Items){
SPFieldUrlValue data = item["Url"] as SPFieldUrlValue;
// now you have data.Description, data.Url
}
There are many such SPField* helper classes, and they are very useful, specially when you have multiple values.
Edit:
For some reason some people believe this way is slower, based on the evidence in the blog post of on Greg's post (even got down voted). This, however, has nothing to do with my answer: a foreach loop creates an Iterator, so it shouldn't access the database 99 more times (on the post they used a for loop to access the first 100 items).

if you are in a feature, the feature is activated at a specific scope, (e.g. Site, Web, WebApplication or Farm).
When you want to access a list from the feature, use the SPFeatureReceiver class to bind an event receiver to your feature. Then, in that class, there are overrides for when the feature activated event is triggered. that override receives a parameter of type SPFeatureReceiverProperties.
from that parameter, you can use get into a site:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
using(SPSite site = properties.Feature.Parent as SPSite) //this depends on scope of feature
{
SPList myList = site.RootWeb.Lists["MyList"];
}
}
for how the iterate that list, see the orther answers

I know this question was asked a very long time ago but I hope I am able to help someone out now :)
This is how I was able to accomplish this,
protected void Page_Load(object sender, EventArgs e)
{
// Get the current domain
var current = HttpContext.Current.Request.Url.Host;
// We need to tell the SPSite object the URL of where our List is stored.
// Make sure you have the right location placed after the 'current' variable
using (SPSite spSite = new SPSite("http://"+current+"/DirectoryForLists/Lists"))
{
// Returns the Web site that is located at the specified server-relative or site-relative URL.
using (SPWeb spWeb = spSite.OpenWeb())
{
//Get our list we created.
SPList list = spWeb.Lists["Name Of the List"];
// Create a new SPQuery object that will hold our CAML query.
SPQuery q = new SPQuery();
// CAML query.
// This allows you to controll how you receieve your data.
// Make the data ASC or DESC. Find more on MSDN.
q.Query = "<OrderBy><FieldRef Name='DESCR' Ascending='TRUE' /></OrderBy>";
// We put our list data into a SP list Item Collection.
// Notice that the CAML query is the only parameter for the GetItems() function.
SPListItemCollection items = list.GetItems(q);
// Here you can loop through your list.
foreach (SPListItem spItem in items)
{
// Your code here.
MessageBox(spItem);
// Get URL column
MessageBox(spItem["URL"]);
// Another Column
MessageBox(spItem["DateTime"]);
}
}
}
}

Below is the best option to iterate
SPList list = web.Lists[listname];
SPQuery query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name='ID' /></OrderBy>";
//Scope="Recursive" retrieves items from all folders and subfolders in a list
query.ViewFields = "<FieldRef Name='" + Lists.MRPLibrary.RebateClaimed + "' /><FieldRef Name='ID'/>";
query.ViewAttributes = "Scope=\"RecursiveAll\"";
query.RowLimit = 100;
do
{
SPListItemCollection items = list.GetItems(query);
foreach (SPListItem listItem in items)
{
}
query.ListItemCollectionPosition = items.ListItemCollectionPosition;
} while (query.ListItemCollectionPosition != null);
}

Below code used to delete all bulk list items and here can skip latest 150 items to delete.
Iterating through SPListItemCollection and very quick time like 3000 items will be delete in 2 mints.
SPList list = web.Lists["DemoDelete"];
SPListItemCollection collListItems = list.GetItems();
var watch = System.Diagnostics.Stopwatch.StartNew();
Console.WriteLine("Start Time: " + watch);
//delete all items uncomment this code
//foreach (SPListItem item in collListItems)
//{
// SPListItem delItem = list.GetItemById(item.ID);
// Console.WriteLine("Item Deleted" + delItem.ID);
// delItem.Delete();
// list.Update();
//}
//skip lastest 150 items
for (int i = collListItems.Count - 150; i >= 0; i--)
{
SPListItem listItem = list.GetItemById(collListItems[i].ID); //collListItems[i];
Console.WriteLine("Item Deleted" + listItem.ID);
listItem.Delete();
list.Update();
}
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine("End Time: " + elapsedMs);

As others have said, you shouldn´t iterate the Items collection directly (especially in large collections). Here´s an alternaltive:
// if you need the whole collection. Otherwise use SPQuery on the list
DataTable dt = list.Items.GetDataTable();
foreach (DataRow row in dt.Rows)
{
...
Then you can do numerous things. If you need to do a check to get only some of the items, ex:
if (row["ContentType"].ToString().Equals("Your contenttype id"))
{
SPListItem item = list.GetItemById((int)row["ID"]);
or use the SpQuery to get your column in the query, like:
SPQuery oQuery = new SPQuery();
oQuery.ViewFields = "<FieldRef Name='UrlColumn'/>";
list.Items.GetItems(oQuery).GetDataTable();
...foreach code...
row["UrlColumn"]

If you are in a x86 environment I've recently discovered an awesomely read-only way to get at the data with MSSQL/OLEDB...
SELECT * FROM OPENROWSET (
'Microsoft.ACE.OLEDB.12.0',
'WSS;IMEX=1;RetrieveIds=Yes;DATABASE=http://sharepoint.lsi.local/ops/;LIST={3DCAF100-44A1-4331-8328-748AA98E36AB};',
'SELECT * FROM list'
)
http://www.connectionstrings.com/sharepoint

BTW
When using OPENROWSET...
IMEX=2 is for Read/Write. IMEX=1 is ReadOnly.
List = [Name] works for me instead of needing to use the list={GUID}.

Related

save List to session already created before using List.Clear()

I have a code similar to this
List<string> list = new List<string>();
for (int i = 0; i < 1000; i++)
{
list.Add(Guid.NewGuid().ToString());
}
foreach (var item in list)
{
lit.Text += string.Format("<p>{0}</p>", item);
}
Session["VarList"] = list;
ViewState["VarList"] = list;
list.Clear();
This might sound like a duplicate to this How to preserve lists already created before using List.Clear() but i need to save that list somehow beacuse my list is is being populated on various methods and i am facing outofmemoryexception. My list contains object a class and that class has a list, DataTable and some properties. Any solution will be very much appreciated i just need this functionality of preserving the data and clearing the list.
Sample for storing a dictionary in session (not direct answer):
List<MyObject> Mylist;
MyList = GetObjects(TheDate);
Dictionary<DateTime> myDictionary = new Dictionary<DateTime>();
myDictionary[TheDate] = MyList;
Session["DateCollections"] = myDictionary;
Sample for retrieving from session (should have null check to be sure it's there):
Dictionary<DateTime> myDictionary = (Dictionary<DateTime>) Session["DateCollections"];
I hope you can continue with this or at least get some inspiration :)

Fill a SharePoint List with a list<splistitem>

I'm working in a webpart. I filter (with caml) a SharePoint list and put my results in a List<SPListItem>.
Now, i need to populate another SharePoint List (I created that list in the same code) and i can't find the way to do that.
List<SPListItem> results = new List<SPListItem>() //results have the result of my query
.
.
.
SPList listFiltered = mySite.Lists[newListName]; //listFiltered is my newlist
SPListItemCollection newListItems = listFiltered.Items; //newListItem are the item from my list
foreach (SPListItem item in results)
{
//I don't know how to send my result to my SharePoint list :(
}
You will need to define your other List, then you can add a new SPListItem to that list with the columns that list contains. I am not sure what results is, if that is a typo or not, but I included that in my answer. You would need to change that if results does not exist.
SPList secondList = web.Lists["MyList"];
foreach(SPListItem item in results)
{
SPListItem Item = secondList.Items.Add();
item["Title"] = companyName
item["DateReceived"] = System.DateTime.Now;
item["Description"] = companyDesc;
item.Update();
}

How do I test for a lookup list item, and add it if it is missing, in SharePoint 2007 via C#?

Hi and thanks for looking!
Background
I am having trouble when I attempt to add a value to a lookup column.
I am using SharePoint 2007 and the app has to run in .NET 2.0. The language is C#. Some lookup columns will allow multiple values.
Question
Using C#, how do I do #'s 2-4 of the following:
Attempt to add a list item to a SP list.
For any lookup columns, check the SP List they are referencing to see if that list contains the value I am attempting to add.
If the value DOES NOT exist in the lookup list, add it.
Associate the newly added lookup value to the list item I was originally trying to add.
I have been googling this, of course, but am still stuck. Here is some code from Microsoft which is a start, but it is still not getting me going (not commented and not intuitive to me):
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://localhost"))
{
using (SPWeb web = site.RootWeb)
{
SPList customerList = web.Lists.TryGetList("Contoso Customers");
SPList orderList = web.Lists.TryGetList("Contoso Orders");
if (customerList != null && orderList != null)
{
SPListItemCollection customers = customerList.Items;
SPListItemCollection orders = orderList.Items;
string fieldName = "CustIDLookup";
if (!orderList.Fields.ContainsField(fieldName))
return;
SPField lookupFld = orderList.Fields.GetField(fieldName);
foreach (SPListItem customer in customers)
{
SPListItem order = orders.Add();
order[SPBuiltInFieldId.Title] = "Thank you!";
order.Update();
SPFieldLookupValue value = new SPFieldLookupValue(customer.ID, customer.ID.ToString());
order[lookupFld.Id] = value.ToString();
order.Update();
}
}
}
}
}
}
}
Even with Microsoft's examples, I can't get any real traction in figuring out how to actually do this.
Your help is GREATLY appreciated.
//untested pseudocode - hope this points you in the right direction
SPList lookupItems = ... // add code here
SPList list = ... // add code here
string lookupFieldName = "LookupValue"; // change to the appropriate value
foreach(SPListItem item in list.Items)
{
string value = (string)item[lookupFieldName];
if(value.Contains("#")) // value containing hash is most likely a lookup value already
{
// use SPFieldLookupValue to get actual value
SPFieldLookupValue currentValue = new SPFieldLookupValue(value);
value = currentValue.LookupValue;
}
// Get the list item (you will need this to find out its id value)
SPListItem lookupItem = GetLookupListItem(lookupList, value);
if(lookupItem == null)
{
//If it doesn't exist, create it
lookupItem = AddNewLookupItem(lookupList, value);
SPFieldLookupValue lookupValue = new SPFieldLookupValue(lookupItem.ID,value));
item["LookupValue"] = lookupValue.ToString();
item.Update();
}
}
SPListItem GetLookupListItem(SPList lookupList, string value)
{
// iterate through list to find item
// use a list query if the list is too big for this to perform well (see here: http://msdn.microsoft.com/en-us/library/ie/ms456030.aspx)
foreach(SPListItem item in lookupList)
{
string itemValue = (string)item[0]; // assuming lookup list has one field of type string containing lookup value
if(value == itemValue)
{
return item;
}
}
return null;
}
SPListItem AddLookupListItem(SPList list, string value)
{
SPListItem newItem = list.Add();
newItem[0] = value;// assuming lookup list has one field of type string containing lookup value
newItem.Update();
}

How do I get a list of ONLY user-created fields in a SharePoint 2007 List via C#?

Hello and thanks for looking!
Background
I currently have a C# method for looping through a SharePoint List Collection and returning a lists of those SP Lists, including a nested list of their columns/SPFields.
Problem
How do I get a list of ONLY user-created fields in a SharePoint 2007 List via C#?
Code so far. . .
SPSite site = SPContext.Current.Site;
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
SPListCollection lists = web.Lists;
var PellaListCollection = new List<PellaListModel>();
foreach (SPList l in lists) {
var PellaList = new PellaListModel();
var PellaListColumns = new List<PellaListColumn>();
foreach (SPField c in l.Fields) {
if (c.Hidden.Equals(false))
{
var type = c.FieldTypeDefinition.TypeName.ToString();
var col = new PellaListColumn
{
ColumnId = c.Id,
ColumnDataType = type,
ColumnTitle = c.Title
};
PellaListColumns.Add(col);
}
}
PellaList.ListColumns = PellaListColumns;
PellaList.ListId = l.ID;
PellaList.ListTitle = l.Title;
PellaList.Description = l.Description;
PellaListCollection.Add(PellaList);
}
web.AllowUnsafeUpdates = false;
return PellaListCollection;
}
As you can see, right now I am filtering by which SPFields are not "Hidden", but this still returns quite a few of the standard SharePoint generated fields with the list. I just need the fields the users have created.
Thanks!
Matt
You are looking for the FromBaseType property of SPField. This property is true if the field is part of the original schema for the List.
Check out this good SharePoint Exchange article for more.
You could potentially test the SourceId in the SPField.
From the MSDN documentation:
"Gets either the namespace that defines a built-in field or, if it a custom field, the GUID that identifies the list or Web site where it was created."

Sorting a DropDownList? - C#, ASP.NET

I'm curious as to the best route (more looking towards simplicity, not speed or efficiency) to sort a DropDownList in C#/ASP.NET - I've looked at a few recommendations but they aren't clicking well with me.
Edit: Folks, I do not have control over how the data comes into the DropDownList - I cannot modify the SQL.
If you get a DataTable with the data, you can create a DataView off of this and then bind the drop down list to that. Your code would look something like...
DataView dvOptions = new DataView(DataTableWithOptions);
dvOptions.Sort = "Description";
ddlOptions.DataSource = dvOptions;
ddlOptions.DataTextField = "Description";
ddlOptions.DataValueField = "Id";
ddlOptions.DataBind();
Your text field and value field options are mapped to the appropriate columnns in the data table you are receiving.
A C# solution for .NET 3.5 (needs System.Linq and System.Web.UI):
public static void ReorderAlphabetized(this DropDownList ddl)
{
List<ListItem> listCopy = new List<ListItem>();
foreach (ListItem item in ddl.Items)
listCopy.Add(item);
ddl.Items.Clear();
foreach (ListItem item in listCopy.OrderBy(item => item.Text))
ddl.Items.Add(item);
}
Call it after you've bound your dropdownlist, e.g. OnPreRender:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
ddlMyDropDown.ReorderAlphabetized();
}
Stick it in your utility library for easy re-use.
Assuming you are running the latest version of the .Net Framework this will work:
List<string> items = GetItemsFromSomewhere();
items.Sort((x, y) => string.Compare(x, y));
DropDownListId.DataSource = items;
DropDownListId.DataBind();
DropDownList takes any IEnumerable as a DataSource.
Just sort it using LINQ.
I usually load a DropDownList with values from a database table, so the easiest way is to sort your results as desired with the ORDER BY clause of your SELECT statement, and then just iterate through the results and dump them into the DropDownList.
Take a look at the this article from CodeProject, which rearranges the content of a dropdownlist. If you are databinding, you will need to run the sorter after the data is bound to the list.
It is recommended to sort the data before databinding it to the DropDownList but in case you can not, this is how you would sort the items in the DropDownList.
First you need a comparison class
Public Class ListItemComparer
Implements IComparer(Of ListItem)
Public Function Compare(ByVal x As ListItem, ByVal y As ListItem) As Integer _
Implements IComparer(Of ListItem).Compare
Dim c As New CaseInsensitiveComparer
Return c.Compare(x.Text, y.Text)
End Function
End Class
Then you need a method that will use this Comparer to sort the DropDownList
Public Shared Sub SortDropDown(ByVal cbo As DropDownList)
Dim lstListItems As New List(Of ListItem)
For Each li As ListItem In cbo.Items
lstListItems.Add(li)
Next
lstListItems.Sort(New ListItemComparer)
cbo.Items.Clear()
cbo.Items.AddRange(lstListItems.ToArray)
End Sub
Finally, call this function with your DropDownList (after it's been databound)
SortDropDown(cboMyDropDown)
P.S. Sorry but my choice of language is VB. You can use http://converter.telerik.com/ to convert the code from VB to C#
Another option is to put the ListItems into an array and sort.
int i = 0;
string[] array = new string[items.Count];
foreach (ListItem li in dropdownlist.items)
{
array[i] = li.ToString();
i++;
}
Array.Sort(array);
dropdownlist.DataSource = array;
dropdownlist.DataBind();
I agree with sorting using ORDER BY when populating with a database query, if all you want is to sort the displayed results alphabetically. Let the database engine do the work of sorting.
However, sometimes you want some other sort order besides alphabetical. For example, you might want a logical sequence like: New, Open, In Progress, Completed, Approved, Closed. In that case, you could add a column to the database table to explicitly set the sort order. Name it something like SortOrder or DisplaySortOrder. Then, in your SQL, you'd ORDER BY the sort order field (without retrieving that field).
What kind of object are you using for databinding? Typically I use Collection<T>, List<T>, or Queue<T> (depending on circumstances). These are relatively easy to sort using a custom delegate. See MSDN documentation on the Comparison(T) delegate.
var list = ddl.Items.Cast<ListItem>().OrderBy(x => x.Text).ToList();
ddl.DataSource = list;
ddl.DataTextField = "Text";
ddl.DataValueField = "Value";
ddl.DataBind();
Try it
-------Store Procedure-----(SQL)
USE [Your Database]
GO
CRATE PROC [dbo].[GetAllDataByID]
#ID int
AS
BEGIN
SELECT * FROM Your_Table
WHERE ID=#ID
ORDER BY Your_ColumnName
END
----------Default.aspx---------
<asp:DropDownList ID="ddlYourTable" runat="server"></asp:DropDownList>
---------Default.aspx.cs-------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<YourTable> table= new List<YourTable>();
YourtableRepository tableRepo = new YourtableRepository();
int conuntryInfoID=1;
table= tableRepo.GetAllDataByID(ID);
ddlYourTable.DataSource = stateInfo;
ddlYourTable.DataTextField = "Your_ColumnName";
ddlYourTable.DataValueField = "ID";
ddlYourTable.DataBind();
}
}
-------LINQ Helper Class----
public class TableRepository
{
string connstr;
public TableRepository()
{
connstr = Settings.Default.YourTableConnectionString.ToString();
}
public List<YourTable> GetAllDataByID(int ID)
{
List<YourTable> table= new List<YourTable>();
using (YourTableDBDataContext dc = new YourTableDBDataContext ())
{
table= dc.GetAllDataByID(ID).ToList();
}
return table;
}
}
I agree with the folks in sorting your data in the model before populating them to the DropDownList, so if you are populating this from a DB, it is a good thing to get them sorted already there using a simple order by clause, it will save you some cycles in the web server, and I am sure the DB will do it so much faster.
If you are populating this from another data source for example, XML file, using LINQ will be a good idea, or even any variation of Array.Sort will be good.
If your data is coming to you as a System.Data.DataTable, call the DataTable's .Select() method, passing in "" for the filterExpression and "COLUMN1 ASC" (or whatever column you want to sort by) for the sort. This will return an array of DataRow objects, sorted as specified, that you can then iterate through and dump into the DropDownList.
List<ListItem> li = new List<ListItem>();
foreach (ListItem list in DropDownList1.Items)
{
li.Add(list);
}
li.Sort((x, y) => string.Compare(x.Text, y.Text));
DropDownList1.Items.Clear();
DropDownList1.DataSource = li;
DropDownList1.DataTextField = "Text";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();
To sort an object datasource that returns a dataset you use the Sort property of the control.
Example usage In the aspx page to sort by ascending order of ColumnName
<asp:ObjectDataSource ID="dsData" runat="server" TableName="Data"
Sort="ColumnName ASC" />
is better if you sort the Source before Binding it to DropDwonList.
but sort DropDownList.Items like this:
Dim Lista_Items = New List(Of ListItem)
For Each item As ListItem In ddl.Items
Lista_Items.Add(item)
Next
Lista_Items.Sort(Function(x, y) String.Compare(x.Text, y.Text))
ddl.Items.Clear()
ddl.Items.AddRange(Lista_Items.ToArray())
(this case i sort by a string(the item's text), it could be the suplier's name, supplier's id)
the Sort() method is for every List(of ) / List<MyType>, you can use it.
You can do it this way is simple
private void SortDDL(ref DropDownList objDDL)
{
ArrayList textList = new ArrayList();
ArrayList valueList = new ArrayList();
foreach (ListItem li in objDDL.Items)
{
textList.Add(li.Text);
}
textList.Sort();
foreach (object item in textList)
{
string value = objDDL.Items.FindByText(item.ToString()).Value;
valueList.Add(value);
}
objDDL.Items.Clear();
for(int i = 0; i < textList.Count; i++)
{
ListItem objItem = new ListItem(textList[i].ToString(), valueList[i].ToString());
objDDL.Items.Add(objItem);
}
}
And call the method this SortDDL(ref yourDropDownList);
and that's it. The data in your dropdownlist will be sorted.
see http://www.codeproject.com/Articles/20131/Sorting-Dropdown-list-in-ASP-NET-using-C#
You can use this JavaScript function:
function sortlist(mylist)
{
var lb = document.getElementById(mylist);
arrTexts = new Array();
arrValues = new Array();
arrOldTexts = new Array();
for(i=0; i<lb.length; i++)
{
arrTexts[i] = lb.options[i].text;
arrValues[i] = lb.options[i].value;
arrOldTexts[i] = lb.options[i].text;
}
arrTexts.sort();
for(i=0; i<lb.length; i++)
{
lb.options[i].text = arrTexts[i];
for(j=0; j<lb.length; j++)
{
if (arrTexts[i] == arrOldTexts[j])
{
lb.options[i].value = arrValues[j];
j = lb.length;
}
}
}
}
Try This:
/// <summary>
/// AlphabetizeDropDownList alphabetizes a given dropdown list by it's displayed text.
/// </summary>
/// <param name="dropDownList">The drop down list you wish to modify.</param>
/// <remarks></remarks>
private void AlphabetizeDropDownList(ref DropDownList dropDownList)
{
//Create a datatable to sort the drop down list items
DataTable machineDescriptionsTable = new DataTable();
machineDescriptionsTable.Columns.Add("DescriptionCode", typeof(string));
machineDescriptionsTable.Columns.Add("UnitIDString", typeof(string));
machineDescriptionsTable.AcceptChanges();
//Put each of the list items into the datatable
foreach (ListItem currentDropDownListItem in dropDownList.Items) {
string currentDropDownUnitIDString = currentDropDownListItem.Value;
string currentDropDownDescriptionCode = currentDropDownListItem.Text;
DataRow currentDropDownDataRow = machineDescriptionsTable.NewRow();
currentDropDownDataRow["DescriptionCode"] = currentDropDownDescriptionCode.Trim();
currentDropDownDataRow["UnitIDString"] = currentDropDownUnitIDString.Trim();
machineDescriptionsTable.Rows.Add(currentDropDownDataRow);
machineDescriptionsTable.AcceptChanges();
}
//Sort the data table by description
DataView sortedView = new DataView(machineDescriptionsTable);
sortedView.Sort = "DescriptionCode";
machineDescriptionsTable = sortedView.ToTable();
//Clear the items in the original dropdown list
dropDownList.Items.Clear();
//Create a dummy list item at the top
ListItem dummyListItem = new ListItem(" ", "-1");
dropDownList.Items.Add(dummyListItem);
//Begin transferring over the items alphabetically from the copy to the intended drop
downlist
foreach (DataRow currentDataRow in machineDescriptionsTable.Rows) {
string currentDropDownValue = currentDataRow["UnitIDString"].ToString().Trim();
string currentDropDownText = currentDataRow["DescriptionCode"].ToString().Trim();
ListItem currentDropDownListItem = new ListItem(currentDropDownText, currentDropDownValue);
//Don't deal with dummy values in the list we are transferring over
if (!string.IsNullOrEmpty(currentDropDownText.Trim())) {
dropDownList.Items.Add(currentDropDownListItem);
}
}
}
This will take a given drop down list with a Text and a Value property of the list item and put them back into the given drop down list.
Best of Luck!
If you are adding options to the dropdown one by one without a dataset and you want to sort it later after adding items, here's a solution:
DataTable dtOptions = new DataTable();
DataColumn[] dcColumns = { new DataColumn("Text", Type.GetType("System.String")),
new DataColumn("Value", Type.GetType("System.String"))};
dtOptions.Columns.AddRange(dcColumns);
foreach (ListItem li in ddlOperation.Items)
{
DataRow dr = dtOptions.NewRow();
dr["Text"] = li.Text;
dr["Value"] = li.Value;
dtOptions.Rows.Add(dr);
}
DataView dv = dtOptions.DefaultView;
dv.Sort = "Text";
ddlOperation.Items.Clear();
ddlOperation.DataSource = dv;
ddlOperation.DataTextField = "Text";
ddlOperation.DataValueField = "Value";
ddlOperation.DataBind();
This would sort the dropdown items in alphabetical order.
If you are using a data bounded DropDownList, just go to the wizard and edit the bounding query by:
Goto the .aspx page (design view).
Click the magic Arrow ">"on the Dropdown List.
Select "Configure Data source".
Click Next.
On the right side of the opened window click "ORDER BY...".
You will have up two there field cariteria to sort by. Select the desired field and click OK, then click Finish.
You may not have access to the SQL, but if you have the DataSet or DataTable, you can certainly call the Sort() method.

Categories