The following control loads the data belonging to a DropDownList control "ddDOBMonth" using a DataTable, a for statement, and two arrays: one for the month text and one for their values.
DataTable itemListMonths = new DataTable();
itemListMonths.Columns.Add(new DataColumn("Value", typeof(int)));
itemListMonths.Columns.Add(new DataColumn("Month", typeof(string)));
DataRow listItemMonth;
listItemMonth = itemListMonths.NewRow();
listItemMonth[0] = -1;
listItemMonth[1] = "Month";
itemListMonths.Rows.Add(listItemMonth);
string[] a_monthText = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
string[] a_monthValues = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };
for (int Value = 0; Value < 12; Value++)
{
listItemMonth = itemListMonths.NewRow();
listItemMonth[0] = a_monthValues[Value];
listItemMonth[1] = a_monthText[Value];
itemListMonths.Rows.Add(listItemMonth);
}
ddDOBMonth.DataSource = itemListMonths;
ddDOBMonth.DataValueField = "Value";
ddDOBMonth.DataTextField = "Month";
ddDOBMonth.DataBind();
ddDOBMonth.SelectedIndex = -1;
Monday, March 17, 2008
Loading data into a DropDownMenu from the codebehind
Labels:
ASP.NET,
C#,
Data Manipulation,
How-To,
Visual Studio
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment