ProductCategoryID ParentCategoryID Level CategoryName
----------------- ---------------- -----------
000 NULL 1 Temp
001 NULL 1 Mac
001001 001 2 Macbook
001001002 001001 3 Cases & bags
001001003 001001 3 Screen Protectors
001001004 001001 3 Cables & Adapters
001001005 001001 3 Batteries & Chargers
001001006 001001 3 Parts & Repair
这是表里面的数据
我要把这按分类绑定在dropdownlist
结果应该是
-|Temp
-|Mac
-|Macbook
--|Cases & bags
--|Screen Protectors
--|Cables & Adapters
--|Parts & Repair求大家指点
解决方案 »
- 怎么样在flexgrid中添加一列combox?
- 用try -catch来把访问dll的代码包含起来,而dll的内容有所改变之后,为什么该try-catch块捕捉不到异常?
- 自定义控件代码中 怎么得到父窗体中的变量?
- 如何删除字符串后的空字符(C#)
- 如何给文件加图片
- 有没有实现WindowsXP采用Royal主题时,状态栏的渐变颜色的控件?
- System.Drawing.Color.DimGray的问题
- C#能否链接mysql
- 关于复合服务器控件呈现问题
- DataGridView(Winform)双击表头时如何不触发这个DataGridView的双击事件
- C#引用外部DLL中的函数:日期格式&编码加密
- 如何解决Openfiledialog使用Dialogresult判定语句后,要点2次按钮才进行操作
--------------------------------
Function A()
{
if(......)
{
A();
}
}
不就解决了?
{
string strSpGroup = "Bjmti_KD_Ken_SEL_Group";
SqlParameter[] spParamGroup = new SqlParameter[1];
spParamGroup[0] =new SqlParameter("@KD_ParentID",SqlDbType.VarChar,36);
spParamGroup[0].Value=pid;
DataSet ds= DbHelperSQL.BjmtiDataSet(strSpGroup,spParamGroup);
foreach(DataRow row in ds.Tables[0].Rows)
{
this.ddlKl.Items.Add(new ListItem(toadd + " " + row["KD_Name"].ToString(),row["KD_ID"].ToString()));
toadd += "─┴";
this.GetArticleCategory(row["KD_ID"].ToString());
toadd = toadd.Substring(0, toadd.Length - 2); //处理层次关系显示
}
}
with cte as
(
select * From Product where Level = 1
Union all
select p.* From Product p join cte on p.ParentCategoryID = cte.ProductCategoryID
)
slect ProductCategoryID,REPLICATE('-',level) + '|' + CategoryName from cte
order by ProductCategoryID
static void Main(string[] args)
{
DataTable dt = BindDataTable();
Console.WriteLine("ProductCategoryID ParentCategoryID Level CategoryName ");
foreach (DataRow dr in dt.Rows)
{
Console.WriteLine(dr["ProductCategoryID"] + " " + dr["ParentCategoryID"] + " " + dr["Level"] + " " + dr["CategoryName"]);
}
SetCateGoryName(dt, null, "ProductCategoryID", "ParentCategoryID", 1, '|', '-', "CategoryName");
Console.WriteLine("--------------------------------------------");
Console.WriteLine("CategoryName "); foreach (DataRow dr in dt.Rows)
{ Console.WriteLine(dr["CategoryName"]);
} }
static void SetCateGoryName(DataTable dt, string parentId,string idField,string parentIdField , int level, char fixedText, char trendsText, string textField)
{
foreach (DataRow row in dt.Rows)
{
if (int.Parse(row["Level"].ToString()) == level)
{
if (string.IsNullOrEmpty(parentId))
{
GenerateText(row, fixedText, trendsText, textField);
string newParentId = row[idField].ToString();
SetCateGoryName(dt, newParentId, idField, parentIdField, level + 1, fixedText, trendsText, textField);
}
else
{
if (row[parentIdField].ToString().Trim() == parentId)
{
GenerateText(row, fixedText, trendsText, textField);
string newParentId = row[idField].ToString();
SetCateGoryName(dt, newParentId, idField, parentIdField, level + 1, fixedText, trendsText, textField);
}
}
}
}
} /// 生成处理符
public static void GenerateText(DataRow dr,char fixedText, char trendsText,string textField)
{
int countTrends = int.Parse(dr["Level"].ToString());
string text = string.Empty;
text = text.PadLeft(countTrends, trendsText);
text = text.PadRight(countTrends+1, fixedText);
dr[textField] = text + dr[textField].ToString();
} private static List<object> GetColumnsListByColumnName(DataTable dt, string parentId, string idField, string parentIdField, int level)
{
if (!dt.Columns.Contains(parentIdField)) return null;
if (!dt.Columns.Contains(idField)) return null; List<object> columnsList = new List<object>();
foreach (DataRow row in dt.Rows)
{
if (int.Parse(row["Level"].ToString()) == level)
{
if (row[parentIdField].ToString() == parentId)
{
columnsList.Add(row[""]);
}
}
}
return columnsList;
} static DataTable BindDataTable()
{
DataTable dt = new DataTable(); dt.Columns.Add("ProductCategoryID");
dt.Columns.Add("ParentCategoryID");
dt.Columns.Add("Level");
dt.Columns.Add("CategoryName"); DataRow dr1 = dt.NewRow();
object[] objs1 = {"000"," NULL","1","Temp"};
dr1.ItemArray = objs1;
dt.Rows.Add(dr1); DataRow dr2 = dt.NewRow();
object[] objs2 = { "001", " NULL", "1", "Mac" };
dr2.ItemArray = objs2;
dt.Rows.Add(dr2); DataRow dr3 = dt.NewRow();
object[] objs3 = { "001001", "001", "2", "Macbook" };
dr3.ItemArray = objs3;
dt.Rows.Add(dr3); DataRow dr4 = dt.NewRow();
object[] objs4 = { "001001002", "001001", "3", "Cases & bags" };
dr4.ItemArray = objs4;
dt.Rows.Add(dr4); DataRow dr5 = dt.NewRow();
object[] objs5 = { "001001003", "001001", "3", "Screen Protectors" };
dr5.ItemArray = objs5;
dt.Rows.Add(dr5); DataRow dr6 = dt.NewRow();
object[] objs6 = { "001001004", "001001", "3", "Cables & Adapters" };
dr6.ItemArray = objs6;
dt.Rows.Add(dr6); DataRow dr7 = dt.NewRow();
object[] objs7 = { "001001005", "001001", "3", "Batteries & Chargers" };
dr7.ItemArray = objs7;
dt.Rows.Add(dr7); DataRow dr8 = dt.NewRow();
object[] objs8 = { "001001006", "001001", "3", "Parts & Repair" };
dr8.ItemArray = objs8;
dt.Rows.Add(dr8);
return dt;
}
临时写的
ProductCategoryID ParentCategoryID Level CategoryName
000 NULL 1 Temp
001 NULL 1 Mac
001001 001 2 Macbook
001001002 001001 3 Cases & bags
001001003 001001 3 Screen Protectors
001001004 001001 3 Cables & Adapters
001001005 001001 3 Batteries & Chargers
001001006 001001 3 Parts & Repair
--------------------------------------------
CategoryName
-|Temp
-|Mac
--|Macbook
---|Cases & bags
---|Screen Protectors
---|Cables & Adapters
---|Batteries & Chargers
---|Parts & Repair