二维数组绑定到Dropdownlist
昨天下午钻了两个小时的牛角尖才悟出来,惭愧。一直以为一维数组可以绑定到dropdownlist,二维数组应该也可以,查了资料才明白数据源必须是实现 System.Collections.IEnumerable 接口(如 System.Data.DataView、System.Collections.ArrayList 和 System.Collections.Hashtable)的对象,不好使用数组.
所以要转换一下才能绑定,把二给数组转换为DataTable或者使用item.insert()方法添加;
把二组数组转换为DataTable进行绑定:
使用item.insert()方法添加:
所以要转换一下才能绑定,把二给数组转换为DataTable或者使用item.insert()方法添加;
把二组数组转换为DataTable进行绑定:
DataTable dt = new DataTable("tbl");
dt.columns.add("0", Typeof(System.string));
dt.columns.add("1", Typeof(System.string));
DataRow row;
for(int i=0; i< aa.length; i++)
{
row = dt.newrow();
row["0"] = aa[i,0];
row["1"] = aa[i,1];
dt.row.add(row);
}
DropDownList1.DataSource=dt;
DropDownList1.DataTextField="0";
DropDownList1.DataValueField="1";
DropDownList1.DataBind();
dt.columns.add("0", Typeof(System.string));
dt.columns.add("1", Typeof(System.string));
DataRow row;
for(int i=0; i< aa.length; i++)
{
row = dt.newrow();
row["0"] = aa[i,0];
row["1"] = aa[i,1];
dt.row.add(row);
}
DropDownList1.DataSource=dt;
DropDownList1.DataTextField="0";
DropDownList1.DataValueField="1";
DropDownList1.DataBind();
使用item.insert()方法添加:
for(int j=0;j<aa.GetLength(0);j++)
{
ddl.Items.Add(new ListItem(aa[j,0],aa[j,1]));
}
{
ddl.Items.Add(new ListItem(aa[j,0],aa[j,1]));
}