数据库中的表的字段字段和数据如下:
categoryID、categoryName、parentID
1 服装类 0
2 电脑类 0
3 少年装 1
4 笔记本 2--parentID为0表示一级分类把这些数据绑定在ListBox中并且显示效果要求如下:
├服装类
├少年装
├电脑类
├笔记本 请高手帮帮忙。
categoryID、categoryName、parentID
1 服装类 0
2 电脑类 0
3 少年装 1
4 笔记本 2--parentID为0表示一级分类把这些数据绑定在ListBox中并且显示效果要求如下:
├服装类
├少年装
├电脑类
├笔记本 请高手帮帮忙。
{
DataTable dt = GetData();
BindData(dt, 0);
}
private void BindData(DataTable dt, int id)
{
DataView dv = new DataView(dt);
dv.RowFilter = "parentid = " + id.ToString();
int m = 0;
foreach (DataRowView drv in dv)
{
m++;
string blank = "";
if (id != 0)
{
for (int i = 0; i < m; i++)
{
blank += "空格";
}
blank += "├";
}
ListItem li = new ListItem();
li.Text = blank + drv["id"].ToString() + drv["name"].ToString();
li.Value = drv["id"].ToString();
this.ListBox1.Items.Add(li);
BindData(dt, Convert.ToInt32(drv["id"]));
}
}
private DataTable GetData()
{
DataTable dt;
dt = new DataTable(); dt.Columns.Add("id");
dt.Columns.Add("parentid");
dt.Columns.Add("name"); DataRow dr;
dr = dt.NewRow();
dr["id"] = "1";
dr["parentid"] = "0";
dr["name"] = "a";
dt.Rows.Add(dr); dr = dt.NewRow();
dr["id"] = "2";
dr["parentid"] = "1";
dr["name"] = "b";
dt.Rows.Add(dr); dr = dt.NewRow();
dr["id"] = "3";
dr["parentid"] = "1";
dr["name"] = "c";
dt.Rows.Add(dr); dr = dt.NewRow();
dr["id"] = "4";
dr["parentid"] = "2";
dr["name"] = "d";
dt.Rows.Add(dr); dr = dt.NewRow();
dr["id"] = "5";
dr["parentid"] = "2";
dr["name"] = "e";
dt.Rows.Add(dr); dr = dt.NewRow();
dr["id"] = "6";
dr["parentid"] = "3";
dr["name"] = "f";
dt.Rows.Add(dr);
return dt;}
id parentid name
1 0 a
2 1 b
3 1 c
4 2 d
5 2 e
6 3 f
运行出来的效果却如下:
1a
├2b
├4d
├5e
├3c
├6fd的parentid并非1是2,它应该在b的下级,e、c、f也都不是在自己父类的下边。
//调用下面函数如下:
BindListBox(ListBox1,ds,"0","categoryID","parentID","categoryName",-1,"");
/// <summary>
/// 把一个无限循环数据表绑定数据到一个ListBox中,并分层显示
/// </summary>
/// <param name="ListBox">绑定的控件</param>
/// <param name="ds">需要的ds</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 BindListBox(System.Web.UI.WebControls.ListBox ListBox,System.Data.DataSet ds,string firstfather_id,string childid,string father_id,string name,int needlayer,string firstnull)
{
if(needlayer>0 || needlayer<0)
{
System.Data.DataView dv=new System.Data.DataView();
dv.Table=ds.Tables[0];
dv.RowFilter = father_id + "=" + firstfather_id;
foreach(System.Data.DataRowView drv in dv)
{
ListBox.Items.Add(new System.Web.UI.WebControls.ListBox(firstnull+drv[name].ToString(),drv[childid].ToString()));
string firstnull1 = "";
if(firstnull.IndexOf("|-")>=0)
{
firstnull1 = " " + firstnull;
}
else
{
firstnull1 = " |-";
}
BindDropDownList2(ListBox,ds,drv[childid].ToString(),childid,father_id,name,needlayer-1,firstnull1);
}
}
}////////已测,可行
BindDropDownList2(ListBox,ds,drv[childid].ToString(),childid,father_id,name,needlayer-1,firstnull1);
改为
BindListBox(ListBox,ds,drv[childid].ToString(),childid,father_id,name,needlayer-1,firstnull1);
这一行有错。应该是
ListBox.Items.Add(new System.Web.UI.WebControls.ListItem(firstnull+drv[name].ToString(),drv[childid].ToString()));此方法可行。谢谢。