This is another variation of loading data into a DropDownList. This time I am using the same dataTable method as my earlier example, however instead of using an array as my data source, I am using another DataTable; One that originates from a TableAdapter from one of the .xsd files in my DAL.
I imagine there is a more streamlined variation of this (why use two DataTables if I don't need to...) but this happens to work just fine for me.
DataTable itemListStates = new DataTable();
itemListStates.Columns.Add(new DataColumn("Value", typeof(int)));
itemListStates.Columns.Add(new DataColumn("Text", typeof(string)));
DataRow listItemState;
listItemState = itemListStates.NewRow();
listItemState[0] = -1;
listItemState[1] = "Select a State";
itemListStates.Rows.Add(listItemState);
StatesTableAdapter statesAdapter = new StatesTableAdapter();
Directory.StatesDataTable states;
states = statesAdapter.GetData();
foreach (Directory.StatesRow statesRow in states)
{
listItemState = itemListStates.NewRow();
listItemState[0] = statesRow.StateAbbreviation;
listItemState[1] = statesRow.StateName;
itemListStates.Rows.Add(listItemState);
}
ddlState.DataSource = itemListStates;
ddlState.DataValueField = "Value";
ddlState.DataTextField = "Text";
ddlState.DataBind();
ddlState.SelectedIndex = -1;
Monday, March 17, 2008
Loading Data into a DropDownList Using DataTables and a Table Adapter
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment