First, bind array arrays to DropDownList
Program code string[] Month =new string[7]{"January", "February", "March", "April", "may", "June", "July"};
This. Dropdownlist1.datasource = Month;
This. Dropdownlist1.databind ();
The first method can only bind a set of data to DropDownList, because drawdonwlist can bind two kinds of data 1 is DataTextField
2 is DataValueField so the first method binds the value of DataTextField after the ==datatextfield value
Second, bind array arrays to DropDownList
program code ArrayList AR = new ArrayList ();
for (int i = 1; I <=12; i++)
{
Ar. ADD (i+ "month");
}
This. Dropdownlist2.datasource = AR;
This. Dropdownlist2.databind ();
Intuitive point of the wording.
Program code
ArrayList ar = new ArrayList ();
Ar. ADD ("January");
Ar. ADD ("February");
Ar. ADD ("March");
Ar. ADD ("April");
..................................................
This. Dropdownlist2.datasource = AR;
This. Dropdownlist2.databind ();
The benefit of the 2nd approach is that the ability to dynamically add elements can be achieved by arraylist.add, for example, there is a DataTable where we want to read a row of data from a DataTable and add it to the ArrayList.
Look at the code shown below me
program code ArrayList AR = new ArrayList ();
DataTable Dt=dataset. Tables[0]
foreach (DataRow dr in Dt. Rows)
{
Ar. ADD (Dr[0]. ToString ());
}
The above code iterates through the foreach statement in a DataTable and adds the value of the first cell in a row of data in the table to ArrayList.
The Third Way, bind Hashtable to DropDownList The advantage of the Hashtable approach is that it can also bind two kinds of data, one is "key, one is" value ", so we can bind two different kinds of data for dropdonwlist.
program code Hashtable Ht = new Hashtable ();
Ht.add ("January", "January");
Ht.add ("February", "February");
Ht.add ("March", "March");
Ht.add ("April", "April");
Ht.add ("may", "May");
Ht.add ("June", "June");
Ht.add ("July", "July");
This. Dropdownlist3.datasource = Ht;
This. Dropdownlist3.datavaluefield = "key";
This. Dropdownlist3.datatextfield = "value";
This. Dropdownlist3.databind ();
4th, bind object to DropDownList
First, add a class with the following structure
Program code public class Classmonth
{
private string _monthen = DateTime.Now.ToString ("MMMM", System.Globalization.CultureInfo.CreateSpecificCulture ("en" ));
private string _MONTHCN = DateTime.Now.ToString ("MMMM", System.Globalization.CultureInfo.CreateSpecificCulture (" ZH-CN "));
Public Classmonth ()
{
MONTHCN = DateTime.Now.ToString ("MMMM", System.Globalization.CultureInfo.CreateSpecificCulture ("ZH-CN"));
Monthen = DateTime.Now.ToString ("MMMM", System.Globalization.CultureInfo.CreateSpecificCulture ("en"));
}
Public Classmonth (String cn,string en)
{
MONTHCN = cn;//Import variable assigns a value to a property
Monthen = en;//Import variable assigns a value to a property
}
public string Monthen//Construction Property
{
Get
{
return _monthen;
}
Set
{
_monthen = value;
}
}
public string MONTHCN//Construction Property
{
Get
{
return _MONTHCN;
}
Set
{
_MONTHCN = value;
}
}
}
Binding method
Program code
ArrayList arlist=new ArrayList ();
Arlist. ADD (New Classmonth ("January", "January"));
Arlist. ADD (New Classmonth ("February", "February"));
Arlist. ADD (New Classmonth ("March", "march"));
Arlist. ADD (New Classmonth ("April", "April"));
Arlist. ADD (New Classmonth ("May", "may"));
This. Dropdownlist4.datasource = arlist;
This. Dropdownlist4.datavaluefield = "Monthen";
This. Dropdownlist4.datatextfield = "MONTHCN";
This. Dropdownlist4.databind ();
"Go" 4 ways to bind data to DropDownList