项目经验之:dropdownlist分组显示数据扩展
dropdownlist操作总结
有时大家在做dropdownlist显示上下级数据时,要有分组的形式显示,如何实现呢,因为我们在3.5的框架中一般做数据的绑定是绑定一组对象数组
所以在绑定到dropdownlist上时是不能有显示成分组的,如何办呢,我们这就应该想到datatable,wq
你可以把它看成一个虚拟的数据表,对他进行上下级关联查询,即可达到分组效果。如图
Code
#region 对象数组转换成dataset
/// <summary>
/// 将对象数组转换成DataSet
/// </summary>
/// <param name="objArr"></param>
/// <returns></returns>
public static DataSet ObjectArrayToDataSet(object[] objArr)
{
if (objArr.Length == 0)
return null;
DataSet ds = CreateDataSet(objArr[0].GetType());
ds = FillDataSet(ds, objArr);
return ds;
}
public static DataSet CreateDataSet(Type t)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
ds.Tables.Add(dt);
PropertyInfo[] pis = t.GetProperties();
foreach (PropertyInfo pi in pis)
{
DataColumn dc = new DataColumn(pi.Name, pi.PropertyType);
dt.Columns.Add(dc);
}
return ds;
}
public static DataSet FillDataSet(DataSet ds, object[] objArr)
{
DataColumnCollection dcs = ds.Tables[0].Columns;
Type t = objArr[0].GetType();
foreach (object obj in objArr)
{
DataRow dr = ds.Tables[0].NewRow();
for (int i = 0; i < dcs.Count; i++)
{
dr[i] = t.InvokeMember(dcs[i].ColumnName, BindingFlags.GetProperty, null, obj, null);
}
ds.Tables[0].Rows.Add(dr);
}
return ds;
}
#endregion
//绑定父节点
public void BindFather()
{
SysUserService.SysOrganizationInfo[] sysOrg = sysUser.GetAllOrg(loginToken, userType); //返回对象数组
drplist.Items.Add(new ListItem("请选择", "0"));
if (sysOrg != null && sysOrg.Length>0)
{
DataTable dt = ArrayToDataSet.ObjectArrayToDataSet(sysOrg).Tables[0]; //数组转换成DataSet
DataRow[] drs = dt.Select("FatherSysOrganizationID= " + 0);//父节点第一个对应的父节点为0
foreach (DataRow dr in drs)
{
string classid = dr["SysOrganizationID"].ToString();
string classname = dr["Name"].ToString();
//顶级分类显示形式
classname = "" + classname; //注意这里你可以用空格代替,空格要是全角的,不然是不能当成 看代的.
drplist.Items.Add(new ListItem(classname, classid));
int sonparentid = int.Parse(classid);
string blank = " ";
//递归子分类方法
BindNode(sonparentid, dt, blank, drplist);
}
drplist.DataBind();
}
}
//绑定子分类
public void BindNode(int parentid, DataTable dt, string blank, DropDownList drplist)
{
DataRow[] drs = dt.Select("FatherSysOrganizationID= " + parentid);
foreach (DataRow dr in drs)
{
string classid = dr["SysOrganizationID"].ToString();
string classname = dr["Name"].ToString();
classname = blank + classname;
drplist.Items.Add(new ListItem(classname, classid));
int sonparentid = int.Parse(classid);
string blank2 = blank + " ";
BindNode(sonparentid, dt, blank2, drplist);//递归操作
}
}
#region 对象数组转换成dataset
/// <summary>
/// 将对象数组转换成DataSet
/// </summary>
/// <param name="objArr"></param>
/// <returns></returns>
public static DataSet ObjectArrayToDataSet(object[] objArr)
{
if (objArr.Length == 0)
return null;
DataSet ds = CreateDataSet(objArr[0].GetType());
ds = FillDataSet(ds, objArr);
return ds;
}
public static DataSet CreateDataSet(Type t)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
ds.Tables.Add(dt);
PropertyInfo[] pis = t.GetProperties();
foreach (PropertyInfo pi in pis)
{
DataColumn dc = new DataColumn(pi.Name, pi.PropertyType);
dt.Columns.Add(dc);
}
return ds;
}
public static DataSet FillDataSet(DataSet ds, object[] objArr)
{
DataColumnCollection dcs = ds.Tables[0].Columns;
Type t = objArr[0].GetType();
foreach (object obj in objArr)
{
DataRow dr = ds.Tables[0].NewRow();
for (int i = 0; i < dcs.Count; i++)
{
dr[i] = t.InvokeMember(dcs[i].ColumnName, BindingFlags.GetProperty, null, obj, null);
}
ds.Tables[0].Rows.Add(dr);
}
return ds;
}
#endregion
//绑定父节点
public void BindFather()
{
SysUserService.SysOrganizationInfo[] sysOrg = sysUser.GetAllOrg(loginToken, userType); //返回对象数组
drplist.Items.Add(new ListItem("请选择", "0"));
if (sysOrg != null && sysOrg.Length>0)
{
DataTable dt = ArrayToDataSet.ObjectArrayToDataSet(sysOrg).Tables[0]; //数组转换成DataSet
DataRow[] drs = dt.Select("FatherSysOrganizationID= " + 0);//父节点第一个对应的父节点为0
foreach (DataRow dr in drs)
{
string classid = dr["SysOrganizationID"].ToString();
string classname = dr["Name"].ToString();
//顶级分类显示形式
classname = "" + classname; //注意这里你可以用空格代替,空格要是全角的,不然是不能当成 看代的.
drplist.Items.Add(new ListItem(classname, classid));
int sonparentid = int.Parse(classid);
string blank = " ";
//递归子分类方法
BindNode(sonparentid, dt, blank, drplist);
}
drplist.DataBind();
}
}
//绑定子分类
public void BindNode(int parentid, DataTable dt, string blank, DropDownList drplist)
{
DataRow[] drs = dt.Select("FatherSysOrganizationID= " + parentid);
foreach (DataRow dr in drs)
{
string classid = dr["SysOrganizationID"].ToString();
string classname = dr["Name"].ToString();
classname = blank + classname;
drplist.Items.Add(new ListItem(classname, classid));
int sonparentid = int.Parse(classid);
string blank2 = blank + " ";
BindNode(sonparentid, dt, blank2, drplist);//递归操作
}
}
青华木园