62,268
社区成员
发帖
与我相关
我的任务
分享传统的递归方式
我自己以前用的分层绑定,效率高的就不知了
/// <summary>
/// 把一个无限循环数据表绑定数据到一个DropDownList下拉列中,并分层显示
/// </summary>
/// <param name="DropDownList">绑定的控件</param>
/// <param name="dt">需要的dt</param>
/// <param name="firstfather_id">第一项的父ID值</param>
/// <param name="childid">子ID字段名</param>
/// <param name="father_id">父ID字段名</param>
/// <param name="name">显示字段名</param>
/// <param name="needlayer">显示的层数,为负值则不限</param>
/// <param name="firstnull">为定值""</param>
public static void BindDropDownList2(System.Web.UI.WebControls.DropDownList DropDownList, System.Data.DataTable dt, string firstfather_id, string childid, string father_id, string name, int needlayer, string firstnull)
{
if (needlayer > 0 || needlayer < 0)
{
System.Data.DataView dv = dt.DefaultView;
dv.RowFilter = father_id + "=" + firstfather_id;
for (int i = 0; i < dv.Count; i++)
{
DropDownList.Items.Add(new System.Web.UI.WebControls.ListItem(firstnull + dv[i][name].ToString(), dv[i][childid].ToString()));
string firstnull1 = "";
if (firstnull.IndexOf("|--") >= 0)
{
firstnull1 = " " + firstnull;
}
else
{
firstnull1 = " |--";
}
BindDropDownList2(DropDownList, dt, dv[i][childid].ToString(), childid, father_id, name, needlayer - 1, firstnull1);
}
dv.Dispose();
}
}