1. 程式人生 > >c#關於DATAtable相關知識

c#關於DATAtable相關知識

        private static DataTable DtOpenMenu = new DataTable("Menu");

 

           //datatable中是否存在列明,如果不存在則新增
            if (!DtOpenMenu.Columns.Contains("TreeTitle")) {
                DataColumn dc = null;
                dc = DtOpenMenu.Columns.Add("TreeTitle", Type.GetType("System.String"));
                dc = DtOpenMenu.Columns.Add("Title", Type.GetType("System.String"));
                dc = DtOpenMenu.Columns.Add("LocationHref", Type.GetType("System.String"));
            }

            //迴圈進行修改
                    for (int i = 0; i < drs2.Length; i++)
                    {
                        DataRow drEmployee = drs2[i];
                        drEmployee.BeginEdit();
                        drEmployee["TreeTitle"] = TreeTitle;
                        drEmployee["Title"] = Title;
                        drEmployee["LocationHref"] = LocationHref;
                        drEmployee.EndEdit();
                    }

             //進行新增新行
                    DtOpenMenu.Rows.Add(new object[] {""+ treeTitle + "", ""+ title + "", ""+ locationHref + "" });

 

 

  #region 複製DataRow的表結構
        public DataTable ToDataTable(DataRow[] rows)
        {
            if (rows == null || rows.Length == 0) return null;
            DataTable tmp = rows[0].Table.Clone();  // 複製DataRow的表結構  
            foreach (DataRow row in rows)
                tmp.Rows.Add(row.ItemArray);  // 將DataRow新增到DataTable中  
            return tmp;
        }

        #endregion