Visit our SharePoint Forum

SharePoint developer? Submit Yourself as Freelancer

Sunday, February 10, 2008

POpulating a Drop-Down with List Data

1. Create a Drop-Down control,in a Webpart or Webapplication.
2. On the Load method, Add the following code.
protected override void OnLoad(EventArgs e)
if (!Page.IsPostBack)
DataSet ds = new DataSet();

SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();

SPList list = myWeb.Lists["ListName"];
DataTable DTable_List = list.Items.GetDataTable();
DTable_List.TableName = "Table1";

DropDownBGroup.DataSource = ds.Tables["Table1"];
DropDownBGroup.DataTextField = "FieldName";
DropDownBGroup.DataValueField = "FieldName";
DropDownBGroup.SelectedIndex = 0;}




Anonymous said...

Nice post on populating dropdowns.Have a question on getting the "FieldName".
I tried this - ds.Tables["Table1"].Columns["Description"];
I was only able to get this example to work using the field reference number - ds.Tables["Table1"].Columns[1]; Is there a way to refer to the list column name and not the number?

Anonymous said...

Disregard the above post. I had changed the field name. Needed to refer to the orginal field name which displays in debug.

Anonymous said...

How to identified table name!
eg: ds.Tables["Table1"].Columns["Description"];
Which table name I have to put & Columns name as default tables of Moss database!
please guide me


Anonymous said...

A very helpful post. Solved my problem

SharePoint Programming