Dua cara untuk menambahkan data ke kontrol DropDownList
Penulis:Eve Cole
Waktu Pembaruan:2009-07-01 16:09:58
Jawabannya:
1 string sql="PILIH * DARI NewsClass ORDER BY ORDERID DESC";
2
3 OleDbConnection samb= OleDbConnection baru();
4
5 samb.ConnectionString=ConnectionString;
6 samb.Buka();
7
8 OleDbCommand cmd = OleDbCommand baru(Sql,Conn);
9 OleDbDataReader dr= cmd.ExecuteReader();
10
11 //将数据添加到 DropDownList
12 sementara(dr.Baca())
13 {
14 ddl.Items.Add(ListItem baru(dr["ClassName"].ToString(),dr["ClassId"].ToString()));
15 }
16 dr.Tutup();
17
18 cmd.Buang();
19 samb.Tutup;
20 samb.Buang();
21
Jawabannya:
1 string sql="PILIH * DARI NewsClass ORDER BY ORDERID DESC";
2
3 OleDbConnection samb= OleDbConnection baru();
4 samb.ConnectionString=ConnectionString;
5 samb.Buka();
6
7 Kumpulan Data ds= Kumpulan Data baru();
8 OleDbDataAdapter da=OleDbDataAdapter baru(sql,sambungan);
9 da.Isi(ds,"ClassTable");
10
11 //将数据添加到 DropDownList
12 ddl.DataSource=ds;
13 ddl.DataTextField = "Nama Kelas";
14 ddl.DataValueField = "ID Kelas";
15 ddl.DataBind();
16
17 ds.Buang();
18 samb.Tutup;
19 samb.Buang();
20
21
http://www.cnblogs.com/meil/archive/2006/10/19/533812.html