private void btnLoad_Click(object sender, EventArgs e)
 {
     btnSave.Enabled = true;
     dgSetTemplate.Rows.Clear();
     ABCardTemplate = UDTTransfer.GetABCardTemplate().OrderBy(x => x.SubjectName).ToList();
     foreach (UDT_ABCardTemplateDefinitionDef data in ABCardTemplate)
     {
         int row = dgSetTemplate.Rows.Add();
         dgSetTemplate.Rows[row].Tag = data;
         dgSetTemplate.Rows[row].Cells[colName.Index].Value = data.SubjectName;
         dgSetTemplate.Rows[row].Cells[colXML.Index].Value  = data.Content;
     }
 }
Example #2
0
        /// <summary>
        /// 將資料寫入 DataTable
        /// </summary>
        private void FillDataToDataTable()
        {
            // 取得對照表
            XElement elmDataMapping = XElement.Parse(Properties.Resources.ABCardTransferDataOldMapping);

            // 讀取 UDT 綜合紀錄表資料
            _ABCardDataList = UDTTransfer.GetABCardDataListByStudentList(_StudentIDList);
            // 分學生
            _ABCardDataDict.Clear();
            foreach (UDT_ABCardDataDef data in _ABCardDataList)
            {
                string sid = data.StudentID.ToString();

                // 取得答案內容
                XElement elm = null;
                if (!string.IsNullOrWhiteSpace(data.Content))
                {
                    elm = XElement.Parse(data.Content);
                }

                if (!_ABCardDataDict.ContainsKey(sid))
                {
                    Dictionary <string, XElement> elms = new Dictionary <string, XElement>();
                    elms.Add(data.SubjectName, elm);
                    _ABCardDataDict.Add(sid, elms);
                }

                if (!_ABCardDataDict[sid].ContainsKey(data.SubjectName))
                {
                    _ABCardDataDict[sid].Add(data.SubjectName, elm);
                }
            }

            try
            {
                // 讀取學生綜合紀錄表UDT內資料,並解析XML放在暫存 Dictionary內。
                // 清空暫存
                _StudABCardDataTempDict.Clear();
                foreach (KeyValuePair <string, Dictionary <string, XElement> > data in _ABCardDataDict)
                {
                    // StudentID
                    string studentId = data.Key;

                    if (!_StudABCardDataTempDict.ContainsKey(studentId))
                    {
                        Dictionary <string, List <Dictionary <string, string> > > tmpDict = new Dictionary <string, List <Dictionary <string, string> > >();
                        // tablename
                        foreach (string tbName in _tableNameList)
                        {
                            List <Dictionary <string, string> > tmpDictList = new List <Dictionary <string, string> >();
                            tmpDict.Add(tbName, tmpDictList);
                        }

                        _StudABCardDataTempDict.Add(studentId, tmpDict);
                    }

                    // 8 大項內容

                    foreach (KeyValuePair <string, XElement> data1 in data.Value)
                    {
                        string   GroupName = data1.Key;
                        XElement elm       = data1.Value;

                        if (GroupName == "2.家庭狀況")
                        {
                            // 處理直系血親
                            List <string> tmpItemListFM = new string[] { "稱謂", "姓名", "存歿", "出生年", "職業", "工作機構", "職稱", "教育程度" }.ToList();

                            // 處理兄弟姊妹
                            List <string> tmpItemListBS = new string[] { "稱謂", "姓名", "畢肆業學校", "出生年次", "備註" }.ToList();


                            // 處理監護人
                            List <string> tmpItemList01 = new string[] { "監護人姓名", "監護人性別", "監護人電話", "監護人關係", "監護人通訊地址" }.ToList();
                            Dictionary <string, string> tmpParentDict = new Dictionary <string, string>();
                            foreach (string name in tmpItemList01)
                            {
                                tmpParentDict.Add(name, "");
                            }


                            // 處理其它項目
                            Dictionary <string, string> tmpOtherDict1 = new Dictionary <string, string>();
                            Dictionary <string, string> tmpOtherDict2 = new Dictionary <string, string>();
                            Dictionary <string, string> tmpOtherDict3 = new Dictionary <string, string>();
                            Dictionary <string, string> tmpOtherDict4 = new Dictionary <string, string>();
                            List <string> tmpItemsList1 = new string[] { "項目年級", "父母關係", "家庭氣氛", "父親管教方式", "母親管教方式", "居住環境", "本人住宿", "經濟狀況", "每星期零用錢(元)", "我覺得是否足夠" }.ToList();
                            // 建立對照
                            foreach (string name in tmpItemsList1)
                            {
                                if (name == "項目年級")
                                {
                                    tmpOtherDict1.Add(name, "1");
                                    tmpOtherDict2.Add(name, "2");
                                    tmpOtherDict3.Add(name, "3");
                                    tmpOtherDict4.Add(name, "4");
                                }
                                else
                                {
                                    tmpOtherDict1.Add(name, "");
                                    tmpOtherDict2.Add(name, "");
                                    tmpOtherDict3.Add(name, "");
                                    tmpOtherDict4.Add(name, "");
                                }
                            }

                            List <Dictionary <string, string> > tmpParentFMDictList = new List <Dictionary <string, string> >();


                            // 處理直系親屬資料,先建立稱謂再比對
                            foreach (XElement elm21 in elm.Elements("Ans"))
                            {
                                string name = elm21.Attribute("name").Value;
                                if (name == "AAB10000001")
                                {
                                    foreach (XElement elms in elm21.Elements("Item"))
                                    {
                                        Dictionary <string, string> tmpParentFMDict = new Dictionary <string, string>();
                                        foreach (string name1 in tmpItemListFM)
                                        {
                                            tmpParentFMDict.Add(name1, "");
                                        }

                                        foreach (XElement elms1 in elms.Elements("Field"))
                                        {
                                            string skey   = elms1.Attribute("key").Value;
                                            string svalue = elms1.Attribute("value").Value;

                                            switch (skey)
                                            {
                                            case "稱謂": tmpParentFMDict["稱謂"] = svalue; break;

                                            case "存、歿": tmpParentFMDict["存歿"] = svalue; break;

                                            case "出生年": tmpParentFMDict["出生年"] = svalue; break;
                                            }
                                        }
                                        tmpParentFMDictList.Add(tmpParentFMDict);
                                    }
                                }
                                // 先找到父與母存儲位置
                                int idx = 0, idxF = 0, idxM = 0;
                                foreach (Dictionary <string, string> dataDict in tmpParentFMDictList)
                                {
                                    foreach (KeyValuePair <string, string> da01 in dataDict)
                                    {
                                        if (da01.Key == "稱謂" && da01.Value == "父")
                                        {
                                            idxF = idx;
                                        }

                                        if (da01.Key == "稱謂" && da01.Value == "母")
                                        {
                                            idxM = idx;
                                        }
                                    }
                                    idx++;
                                }

                                // 父教育程度
                                if (name == "AAB10000002")
                                {
                                    tmpParentFMDictList[idxF]["教育程度"] = elm21.Attribute("value").Value;
                                }


                                // 母教育程度
                                if (name == "AAB10000003")
                                {
                                    tmpParentFMDictList[idxM]["教育程度"] = elm21.Attribute("value").Value;
                                }

                                // 家長其它
                                if (name == "AAB10000004")
                                {
                                    XElement elmF = null;
                                    XElement elmM = null;
                                    foreach (XElement elmsss in elm21.Elements("Item"))
                                    {
                                        foreach (XElement elmsss1 in elmsss.Elements("Field"))
                                        {
                                            string sssname  = elmsss1.Attribute("key").Value;
                                            string sssvalue = elmsss1.Attribute("value").Value;

                                            if (sssvalue == "父")
                                            {
                                                elmF = elmsss;
                                            }

                                            if (sssvalue == "母")
                                            {
                                                elmM = elmsss;
                                            }
                                        }
                                    }

                                    if (elmF != null)
                                    {
                                        foreach (XElement elmd in elmF.Elements("Field"))
                                        {
                                            string sssname  = elmd.Attribute("key").Value;
                                            string sssvalue = elmd.Attribute("value").Value;
                                            switch (sssname)
                                            {
                                            case "姓名": tmpParentFMDictList[idxF]["姓名"] = sssvalue; break;

                                            case "職業": tmpParentFMDictList[idxF]["職業"] = sssvalue; break;

                                            case "工作機構": tmpParentFMDictList[idxF]["工作機構"] = sssvalue; break;

                                            case "職稱": tmpParentFMDictList[idxF]["職稱"] = sssvalue; break;
                                            }
                                        }
                                    }

                                    if (elmM != null)
                                    {
                                        foreach (XElement elmd in elmF.Elements("Field"))
                                        {
                                            string sssname  = elmd.Attribute("key").Value;
                                            string sssvalue = elmd.Attribute("value").Value;
                                            switch (sssname)
                                            {
                                            case "姓名": tmpParentFMDictList[idxM]["姓名"] = sssvalue; break;

                                            case "職業": tmpParentFMDictList[idxM]["職業"] = sssvalue; break;

                                            case "工作機構": tmpParentFMDictList[idxM]["工作機構"] = sssvalue; break;

                                            case "職稱": tmpParentFMDictList[idxM]["職稱"] = sssvalue; break;
                                            }
                                        }
                                    }
                                }
                            }
                            // 加入 DataTable
                            foreach (Dictionary <string, string> daP in tmpParentFMDictList)
                            {
                                _StudABCardDataTempDict[studentId]["家庭狀況-直系血親"].Add(daP);
                            }

                            foreach (XElement elm24 in elm.Elements("Ans"))
                            {
                                string value = "";
                                if (elm24.Attribute("value") != null)
                                {
                                    value = elm24.Attribute("value").Value;
                                }

                                switch (elm24.Attribute("name").Value)
                                {
                                // 兄弟姊妹
                                case "AAB10000011":
                                    if (elm24.Element("Item") != null)
                                    {
                                        foreach (XElement elms in elm24.Elements("Item"))
                                        {
                                            Dictionary <string, string> tmpParentBSDict = new Dictionary <string, string>();
                                            foreach (string name in tmpItemListBS)
                                            {
                                                tmpParentBSDict.Add(name, "");
                                            }
                                            foreach (XElement elms1 in elms.Elements("Field"))
                                            {
                                                string skey   = elms1.Attribute("key").Value;
                                                string svalue = elms1.Attribute("value").Value;

                                                switch (skey)
                                                {
                                                case "稱謂": tmpParentBSDict["稱謂"] = svalue; break;

                                                case "姓名": tmpParentBSDict["姓名"] = svalue; break;

                                                case "畢(肆)業學校": tmpParentBSDict["畢肆業學校"] = svalue; break;

                                                case "出生年次": tmpParentBSDict["出生年次"] = svalue; break;

                                                case "備註": tmpParentBSDict["備註"] = svalue; break;
                                                }
                                            }
                                            _StudABCardDataTempDict[studentId]["家庭狀況-兄弟姊妹"].Add(tmpParentBSDict);
                                        }
                                    }
                                    break;

                                // 監護人
                                case "AAB10000005": tmpParentDict["監護人姓名"] = value; break;

                                case "AAB10000006": tmpParentDict["監護人性別"] = value; break;

                                case "AAB10000009": tmpParentDict["監護人電話"] = value; break;

                                case "AAB10000007": tmpParentDict["監護人關係"] = value; break;

                                case "AAB10000008": tmpParentDict["監護人通訊地址"] = value; break;

                                // 其它
                                case "AAB10000012": tmpOtherDict1["父母關係"] = value; break;

                                case "AAB10000013": tmpOtherDict2["父母關係"] = value; break;

                                case "AAB10000014": tmpOtherDict3["父母關係"] = value; break;

                                case "AAB10000015": tmpOtherDict4["父母關係"] = value; break;

                                case "AAB10000020": tmpOtherDict1["父親管教方式"] = value; break;

                                case "AAB10000021": tmpOtherDict2["父親管教方式"] = value; break;

                                case "AAB10000022": tmpOtherDict3["父親管教方式"] = value; break;

                                case "AAB10000023": tmpOtherDict4["父親管教方式"] = value; break;

                                case "AAB10000032": tmpOtherDict1["本人住宿"] = value; break;

                                case "AAB10000033": tmpOtherDict2["本人住宿"] = value; break;

                                case "AAB10000034": tmpOtherDict3["本人住宿"] = value; break;

                                case "AAB10000035": tmpOtherDict4["本人住宿"] = value; break;

                                case "AAB10000024": tmpOtherDict1["母親管教方式"] = value; break;

                                case "AAB10000025": tmpOtherDict2["母親管教方式"] = value; break;

                                case "AAB10000026": tmpOtherDict3["母親管教方式"] = value; break;

                                case "AAB10000027": tmpOtherDict4["母親管教方式"] = value; break;

                                case "AAB10000044": tmpOtherDict1["我覺得是否足夠"] = value; break;

                                case "AAB10000045": tmpOtherDict2["我覺得是否足夠"] = value; break;

                                case "AAB10000046": tmpOtherDict3["我覺得是否足夠"] = value; break;

                                case "AAB10000047": tmpOtherDict4["我覺得是否足夠"] = value; break;

                                case "AAB10000040": tmpOtherDict1["每星期零用錢(元)"] = value; break;

                                case "AAB10000041": tmpOtherDict2["每星期零用錢(元)"] = value; break;

                                case "AAB10000042": tmpOtherDict3["每星期零用錢(元)"] = value; break;

                                case "AAB10000043": tmpOtherDict4["每星期零用錢(元)"] = value; break;

                                case "AAB10000028": tmpOtherDict1["居住環境"] = value; break;

                                case "AAB10000029": tmpOtherDict2["居住環境"] = value; break;

                                case "AAB10000030": tmpOtherDict3["居住環境"] = value; break;

                                case "AAB10000031": tmpOtherDict4["居住環境"] = value; break;

                                case "AAB10000016": tmpOtherDict1["家庭氣氛"] = value; break;

                                case "AAB10000017": tmpOtherDict2["家庭氣氛"] = value; break;

                                case "AAB10000018": tmpOtherDict3["家庭氣氛"] = value; break;

                                case "AAB10000019": tmpOtherDict4["家庭氣氛"] = value; break;

                                case "AAB10000036": tmpOtherDict1["經濟狀況"] = value; break;

                                case "AAB10000037": tmpOtherDict2["經濟狀況"] = value; break;

                                case "AAB10000038": tmpOtherDict3["經濟狀況"] = value; break;

                                case "AAB10000039": tmpOtherDict4["經濟狀況"] = value; break;
                                }
                            }

                            // 加入 Datatable


                            _StudABCardDataTempDict[studentId]["家庭狀況-監護人"].Add(tmpParentDict);
                            _StudABCardDataTempDict[studentId]["家庭狀況-其它項目"].Add(tmpOtherDict1);
                            _StudABCardDataTempDict[studentId]["家庭狀況-其它項目"].Add(tmpOtherDict2);
                            _StudABCardDataTempDict[studentId]["家庭狀況-其它項目"].Add(tmpOtherDict3);
                            _StudABCardDataTempDict[studentId]["家庭狀況-其它項目"].Add(tmpOtherDict4);
                        }
                        else if (GroupName == "3.學習狀況")
                        {
                            // 年級,欄位名稱,value
                            Dictionary <string, Dictionary <string, string> > _grData3Dict = new Dictionary <string, Dictionary <string, string> >();
                            foreach (XElement elm3 in elm.Elements("Ans"))
                            {
                                foreach (XElement elmMap in (from da in elmDataMapping.Elements("Item") where da.Attribute("Key").Value == elm3.Attribute("name").Value select da))
                                {
                                    string AType    = elmMap.Attribute("AType").Value;
                                    string col3Name = elmMap.Attribute("ColumnName").Value;
                                    if (!_grData3Dict.ContainsKey(AType))
                                    {
                                        Dictionary <string, string> tmp3Dict = new Dictionary <string, string>();
                                        _grData3Dict.Add(AType, tmp3Dict);
                                    }

                                    string value3 = "";
                                    if (elm3.Elements("Item").Count() == 0)
                                    {   // 單選
                                        if (elm3.Attribute("value") != null)
                                        {
                                            value3 = elm3.Attribute("value").Value;
                                        }
                                    }
                                    else
                                    {
                                        // 多選
                                        List <string> tmp3List = new List <string>();
                                        foreach (XElement tmp3Elm in elm3.Elements("Item"))
                                        {
                                            tmp3List.Add(tmp3Elm.Attribute("value").Value);
                                        }
                                        if (tmp3List.Count > 0)
                                        {
                                            value3 = string.Join(",", tmp3List.ToArray());
                                        }
                                    }

                                    if (!_grData3Dict[AType].ContainsKey(col3Name))
                                    {
                                        _grData3Dict[AType].Add(col3Name, value3);
                                    }
                                }
                            }

                            // 放入暫存 Dict
                            foreach (KeyValuePair <string, Dictionary <string, string> > data3 in _grData3Dict)
                            {
                                Dictionary <string, string> tmp3DataDict = new Dictionary <string, string>();
                                tmp3DataDict.Add("項目年級", data3.Key);

                                foreach (KeyValuePair <string, string> data31 in data3.Value)
                                {
                                    if (!tmp3DataDict.ContainsKey(data31.Key))
                                    {
                                        tmp3DataDict.Add(data31.Key, data31.Value);
                                    }
                                }
                                _StudABCardDataTempDict[studentId]["學習狀況"].Add(tmp3DataDict);
                            }
                        }
                        else if (GroupName == "5.自我認識")
                        {
                            string tbName5 = "自我認識";
                            foreach (XElement elm5 in elm.Element("Ans").Elements("Item"))
                            {
                                Dictionary <string, string> tmpDict5 = new Dictionary <string, string>();
                                foreach (XElement elm51 in elm5.Elements("Field"))
                                {
                                    foreach (XElement elmMap in (from da in elmDataMapping.Elements("Item") where da.Attribute("TableName").Value == tbName5 select da))
                                    {
                                        string columnName5 = elmMap.Attribute("ColumnName").Value;
                                        if (elm51.Attribute("key").Value == elmMap.Attribute("QuestionName").Value)
                                        {
                                            if (!tmpDict5.ContainsKey(columnName5))
                                            {
                                                tmpDict5.Add(columnName5, elm51.Attribute("value").Value);
                                            }
                                        }
                                    }
                                }
                                _StudABCardDataTempDict[studentId][tbName5].Add(tmpDict5);
                            }
                        }
                        else
                        {
                            // 解析原有綜合紀錄表
                            foreach (XElement elm1 in elm.Elements("Ans"))
                            {
                                // 學生綜合紀錄答案key
                                string name       = elm1.Attribute("name").Value;
                                string columnName = "";

                                // 比對對照表名稱與答案名稱,找出相對答案
                                foreach (string tableName in _tableName1List)
                                {
                                    Dictionary <string, string> tmpData1 = new Dictionary <string, string>();

                                    foreach (XElement elmMap in (from da in elmDataMapping.Elements("Item") where da.Attribute("Key").Value == name && da.Attribute("TableName").Value == tableName select da))
                                    {
                                        columnName = elmMap.Attribute("ColumnName").Value;
                                        if (!tmpData1.ContainsKey(columnName))
                                        {
                                            string value = "";
                                            if (elm1.Attribute("value") != null)
                                            {
                                                value = elm1.Attribute("value").Value;
                                            }
                                            tmpData1.Add(columnName, value);
                                        }
                                    }

                                    _StudABCardDataTempDict[studentId][tableName].Add(tmpData1);
                                }
                            }

                            // 這段在解析有 CheckBox 類,不同儲存格式
                            List <string> ansListAAA10000011 = new List <string>();
                            List <string> ansListAAA10000012 = new List <string>();
                            List <string> andListAAG10000001 = new List <string>();
                            List <string> andListAAG10000002 = new List <string>();
                            List <string> andListAAG10000003 = new List <string>();
                            List <string> andListAAG10000004 = new List <string>();
                            foreach (XElement elm1 in elm.Elements("Ans"))
                            {
                                string name = elm1.Attribute("name").Value;

                                // 生理缺陷
                                if (name == "AAA10000011")
                                {
                                    ansListAAA10000011 = elmConvertCheckItemToList(elm1);
                                }

                                // 曾患特殊疾病
                                if (name == "AAA10000012")
                                {
                                    ansListAAA10000012 = elmConvertCheckItemToList(elm1);
                                }

                                // 升學意願
                                if (name == "AAG10000001")
                                {
                                    andListAAG10000001 = elmConvertCheckItemToList(elm1);
                                }
                                // 就業意願
                                if (name == "AAG10000002")
                                {
                                    andListAAG10000002 = elmConvertCheckItemToList(elm1);
                                }
                                // 職業訓練
                                if (name == "AAG10000003")
                                {
                                    andListAAG10000003 = elmConvertCheckItemToList(elm1);
                                }
                                // 受訓地區
                                if (name == "AAG10000004")
                                {
                                    andListAAG10000004 = elmConvertCheckItemToList(elm1);
                                }
                            }
                            foreach (Dictionary <string, string> dassDict in _StudABCardDataTempDict[studentId]["本人概況"])
                            {
                                if (dassDict.ContainsKey("生理缺陷"))
                                {
                                    if (ansListAAA10000011.Count > 0)
                                    {
                                        dassDict["生理缺陷"] = string.Join(",", ansListAAA10000011.ToArray());
                                    }
                                }
                                if (dassDict.ContainsKey("曾患特殊疾病"))
                                {
                                    if (ansListAAA10000012.Count > 0)
                                    {
                                        dassDict["曾患特殊疾病"] = string.Join(",", ansListAAA10000012.ToArray());
                                    }
                                }
                            }

                            foreach (Dictionary <string, string> dassDict in _StudABCardDataTempDict[studentId]["畢業後計畫"])
                            {
                                if (dassDict.ContainsKey("升學意願"))
                                {
                                    if (andListAAG10000001.Count > 0)
                                    {
                                        dassDict["升學意願"] = string.Join(",", andListAAG10000001.ToArray());
                                    }
                                }

                                if (dassDict.ContainsKey("就業意願"))
                                {
                                    if (andListAAG10000002.Count > 0)
                                    {
                                        dassDict["就業意願"] = string.Join(",", andListAAG10000002.ToArray());
                                    }
                                }

                                if (dassDict.ContainsKey("職業訓練"))
                                {
                                    if (andListAAG10000003.Count > 0)
                                    {
                                        dassDict["職業訓練"] = string.Join(",", andListAAG10000003.ToArray());
                                    }
                                }

                                if (dassDict.ContainsKey("受訓地區"))
                                {
                                    if (andListAAG10000004.Count > 0)
                                    {
                                        dassDict["受訓地區"] = string.Join(",", andListAAG10000004.ToArray());
                                    }
                                }
                            }
                        }
                    }
                }


                // 讀取暫存 Dictionary將綜合紀錄表資料放入 DataTable 內。
                foreach (KeyValuePair <string, Dictionary <string, List <Dictionary <string, string> > > > data in _StudABCardDataTempDict)
                {
                    // StudentID
                    string studentId     = data.Key;
                    string StudentNumber = _studRecDict[studentId].StudentNumber;
                    string StudentName   = _studRecDict[studentId].Name;
                    string StudentStatus = _studRecDict[studentId].Status.ToString();

                    foreach (KeyValuePair <string, List <Dictionary <string, string> > > data1 in data.Value)
                    {
                        string TableName = data1.Key;
                        if (_dtTablesDict.ContainsKey(TableName))
                        {
                            if (TableName == "自我認識" || TableName == "學習狀況" || TableName == "家庭狀況-其它項目" || TableName == "家庭狀況-兄弟姊妹" || TableName == "家庭狀況-直系血親")
                            {
                                foreach (Dictionary <string, string> data2 in data1.Value)
                                {
                                    DataRow dr = _dtTablesDict[TableName].NewRow();
                                    foreach (KeyValuePair <string, string> data3 in data2)
                                    {
                                        dr[data3.Key] = data3.Value;
                                    }

                                    dr["學號"]   = StudentNumber;
                                    dr["學生姓名"] = StudentName;
                                    dr["學生狀態"] = StudentStatus;
                                    _dtTablesDict[TableName].Rows.Add(dr);
                                }
                            }
                            else
                            {
                                DataRow dr = _dtTablesDict[TableName].NewRow();
                                foreach (Dictionary <string, string> data2 in data1.Value)
                                {
                                    foreach (KeyValuePair <string, string> data3 in data2)
                                    {
                                        dr[data3.Key] = data3.Value;
                                    }
                                }
                                dr["學號"]   = StudentNumber;
                                dr["學生姓名"] = StudentName;
                                dr["學生狀態"] = StudentStatus;
                                _dtTablesDict[TableName].Rows.Add(dr);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            List <UDT_ABCardTemplateDefinitionDef> updateList  = new List <UDT_ABCardTemplateDefinitionDef>();
            List <UDT_ABCardTemplateDefinitionDef> insertrList = new List <UDT_ABCardTemplateDefinitionDef>();
            List <UDT_ABCardTemplateDefinitionDef> deleteList  = new List <UDT_ABCardTemplateDefinitionDef>();
            List <string> UIDList = new List <string>();

            foreach (DataGridViewRow dgvr in dgSetTemplate.Rows)
            {
                if (dgvr.IsNewRow)
                {
                    continue;
                }
                UDT_ABCardTemplateDefinitionDef data = dgvr.Tag as UDT_ABCardTemplateDefinitionDef;
                if (data != null)
                {
                    if (dgvr.Cells[colXML.Index].Value != null)
                    {
                        data.Content = dgvr.Cells[colXML.Index].Value.ToString();
                    }
                    else
                    {
                        data.Content = "";
                    }

                    if (dgvr.Cells[colName.Index].Value != null)
                    {
                        data.SubjectName = dgvr.Cells[colName.Index].Value.ToString();
                    }
                    else
                    {
                        data.SubjectName = "";
                    }

                    if (!string.IsNullOrEmpty(data.UID))
                    {
                        updateList.Add(data);
                        UIDList.Add(data.UID);
                    }
                }
                else
                {
                    UDT_ABCardTemplateDefinitionDef dataI = new UDT_ABCardTemplateDefinitionDef();
                    if (dgvr.Cells[colXML.Index].Value != null)
                    {
                        dataI.Content = dgvr.Cells[colXML.Index].Value.ToString();
                    }
                    else
                    {
                        dataI.Content = "";
                    }

                    if (dgvr.Cells[colName.Index].Value != null)
                    {
                        dataI.SubjectName = dgvr.Cells[colName.Index].Value.ToString();
                    }
                    else
                    {
                        dataI.SubjectName = "";
                    }

                    insertrList.Add(dataI);
                }
            }

            // delete
            foreach (UDT_ABCardTemplateDefinitionDef data in ABCardTemplate)
            {
                if (!UIDList.Contains(data.UID))
                {
                    data.Deleted = true;
                    deleteList.Add(data);
                }
            }

            if (deleteList.Count > 0)
            {
                UDTTransfer.DeleteABCardTemplate(deleteList);
            }

            if (updateList.Count > 0 || insertrList.Count > 0)
            {
                if (updateList.Count > 0)
                {
                    UDTTransfer.UpdateABCardTemplate(updateList);
                }

                if (insertrList.Count > 0)
                {
                    UDTTransfer.InsertABCardTemplate(insertrList);
                }

                FISCA.Presentation.Controls.MsgBox.Show("儲存完成.");
            }

            btnSave.Enabled = false;
        }