2 u012465853 u012465853 于 2013.10.16 17:45 提问

c#中读取目录树xml文件并显示到dataGridView中

读取xml,但是出来的表格各级目录的位置不对应,差一行
CSDN移动问答

是因为我每列都创建了一个新的datarow,我想问问怎么能够读出来都对应整齐?
还有就是如果我不清楚这个xml目录树文件有几个等级 ,怎么用程序自动读出来并建立列,现在都是我手动建立的?
本人菜鸟一枚,正在学习中,还望大神们帮助~
CSDN移动问答

这是xml文件的结构图 资源目录是一级目录,floderlist中是剩下的各级目录,每个都有FolderCode和FolderParentCode,用来确定目录级别。layerlist是图层,也都有FolderParentCode,用来确定在哪个目录下。
private void button1_Click(object sender, EventArgs e)
{
DataSet ADataSet = new DataSet(); //创建一个DataSet
//把XML文件读取到DataSet.Tables中去,实际在Tables[0]中.
ADataSet.ReadXml("D://XMLFile1.xml");
DataTable tb1 = ADataSet.Tables[3];
DataTable tb2 = ADataSet.Tables[5];
DataTable tb0 = ADataSet.Tables1;

        DataTable w = new DataTable();   //创建新的列
        w.Columns.Add("一级", typeof(string));
        w.Columns.Add("二级", typeof(string));
        w.Columns.Add("三级", typeof(string));
        w.Columns.Add("四级", typeof(string));
        w.Columns.Add("五级", typeof(string));
        w.Columns.Add("六级", typeof(string));
        w.Columns.Add("图层", typeof(string));
        w.Columns.Add("五1级", typeof(string));
        w.Columns.Add("六级1", typeof(string));
        w.Columns.Add("七级", typeof(string));
        w.Columns.Add("八级", typeof(string));
        w.Columns.Add("九级", typeof(string));


        DataRow one = w.NewRow();
        one["一级"] = tb0.Rows[0]["FolderName"];
        w.Rows.Add(one);


        foreach (DataRow myRow0 in tb1.Rows)
        {

            if (myRow0["FolderParentCode"].ToString() == "")
            {
                DataRow two = w.NewRow();
                two["二级"] = myRow0["FolderName"];
                two["五1级"] = myRow0["FolderCode"];
                w.Rows.Add(two);
                foreach (DataRow myRow1 in tb1.Rows)
                {

                    if (myRow1["FolderParentCode"].ToString() == two["五1级"].ToString())
                    {
                        DataRow thr = w.NewRow();
                        thr["三级"] = myRow1["FolderName"];
                        thr["六级1"] = myRow1["FolderCode"];
                        w.Rows.Add(thr);
                        foreach (DataRow myRow2 in tb1.Rows)
                        {
                            if (myRow2["FolderParentCode"].ToString() == thr["六级1"].ToString())
                            {
                                DataRow four = w.NewRow();
                                four["四级"] = myRow2["FolderName"];
                                four["七级"] = myRow2["FolderCode"];
                                w.Rows.Add(four);
                                foreach (DataRow myRow3 in tb1.Rows)
                                {
                                    if (myRow3["FolderParentCode"].ToString() == four["七级"].ToString())
                                    {
                                        DataRow five = w.NewRow();
                                        five["五级"] = myRow3["FolderName"];
                                        five["八级"] = myRow3["FolderCode"];
                                        w.Rows.Add(five);
                                        foreach (DataRow myRow5 in tb1.Rows)
                                        {
                                            if (myRow5["FolderParentCode"].ToString() == five["八级"].ToString())
                                            {
                                                DataRow seven = w.NewRow();
                                                seven["六级"] = myRow5["FolderName"];
                                                seven["九级"] = myRow5["FolderCode"];
                                                w.Rows.Add(seven);
                                                foreach (DataRow myRow4 in tb2.Rows)
                                                {
                                                    if (myRow4["FolderCode"].ToString() == seven["九级"].ToString())
                                                    {
                                                        DataRow eight = w.NewRow();
                                                        eight["图层"] = myRow4["F_Name"];
                                                        w.Rows.Add(eight);
                                                    }
                                                    else
                                                        if (myRow4["FolderCode"].ToString() == five["八级"].ToString())
                                                        {
                                                            DataRow six = w.NewRow();
                                                            six["图层"] = myRow4["F_Name"];
                                                            w.Rows.Add(six);

                                                        }

                                                }

                                            }
                                        }





                                    }
                                }

                            }
                        }
                    }

                }
            }

        }
        w.Columns.Remove(w.Columns["五1级"]);
        w.Columns.Remove(w.Columns["六级1"]);
        w.Columns.Remove(w.Columns["七级"]);
        w.Columns.Remove(w.Columns["八级"]);
        w.Columns.Remove(w.Columns["九级"]);
        this.dataGridView1.DataSource = tb2.DefaultView;
        this.dataGridView2.DataSource = w.DefaultView;
    }
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!