private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                btnSave.Enabled = false;
                List <UDT.HistoricalRecords> ssData = new List <UDT.HistoricalRecords>();
                foreach (DataGridViewRow drv in dgData.Rows)
                {
                    UDT.HistoricalRecords hr = drv.Tag as UDT.HistoricalRecords;

                    if (hr != null)
                    {
                        hr.Rank = null;
                        if (drv.Cells[colRank.Index].Value != null)
                        {
                            int rank;

                            int.TryParse(drv.Cells[colRank.Index].Value.ToString(), out rank);
                            hr.Rank      = rank;
                            hr.CreatedBy = _userAccount;
                        }
                        ssData.Add(hr);
                    }
                }
                if (ssData.Count > 0)
                {
                    ssData.SaveAll();
                }
                FISCA.Presentation.Controls.MsgBox.Show("儲存完成");
                LoadData();
            }
            catch (Exception ex)
            {
                FISCA.Presentation.Controls.MsgBox.Show("儲存失敗," + ex.Message);
                btnSave.Enabled = true;
                return;
            }
            finally
            {
            }
        }
        private void LoadDataToGridView(bool isAll)
        {
            int rowCount = 0;

            dgData.Rows.Clear();
            if (isAll)
            {
            }
            else
            {
                if (_EventsDict.ContainsKey(_selectEventID))
                {
                    UDT.Events ev     = _EventsDict[_selectEventID];
                    string     gpName = "";

                    if (_GroupTypesDict.ContainsKey(ev.RefGroupTypeId))
                    {
                        gpName = _GroupTypesDict[ev.RefGroupTypeId].Name;
                    }

                    // 列隊
                    if (_teamDict.ContainsKey(_selectEventID) && ev.IsTeam)
                    {
                        foreach (UDT.Teams t in _teamDict[_selectEventID])
                        {
                            int rowIdx = dgData.Rows.Add();

                            UDT.HistoricalRecords hr = null;

                            foreach (UDT.HistoricalRecords rec in _HistoricalRecordsList)
                            {
                                if (rec.RefEventId == _selectEventID && rec.TeamName == t.Name)
                                {
                                    hr = rec;
                                }
                            }

                            if (hr == null)
                            {
                                hr            = new UDT.HistoricalRecords();
                                hr.RefEventId = _selectEventID;
                                hr.TeamName   = t.Name;
                            }


                            dgData.Rows[rowIdx].Cells[colSchoolYear.Index].Value    = ev.SchoolYear;
                            dgData.Rows[rowIdx].Cells[colEventCategory.Index].Value = ev.Category;
                            dgData.Rows[rowIdx].Cells[colEventName.Index].Value     = ev.Name;
                            dgData.Rows[rowIdx].Cells[colGroupType.Index].Value     = gpName;
                            dgData.Rows[rowIdx].Cells[colIsTeam.Index].Value        = ev.IsTeam ? "是" : "否";
                            dgData.Rows[rowIdx].Cells[colTeamName.Index].Value      = t.Name;

                            if (hr.Rank.HasValue)
                            {
                                dgData.Rows[rowIdx].Cells[colRank.Index].Value = hr.Rank.Value;
                            }

                            if (_teamPlayerDict.ContainsKey(t.UID))
                            {
                                List <string> name = new List <string>();
                                List <string> jSON = new List <string>();

                                foreach (UDT.Players p in _teamPlayerDict[t.UID])
                                {
                                    name.Add(p.Name);
                                    jSON.Add(parsePlayerToJSonString(p));
                                }
                                dgData.Rows[rowIdx].Cells[colPlayer.Index].Value = string.Join(",", name.ToArray());

                                if (jSON.Count > 0)
                                {
                                    hr.Players = $"[{string.Join(",", jSON.ToArray())}]";
                                }
                            }

                            dgData.Rows[rowIdx].Tag = hr;

                            rowCount++;
                        }
                    }

                    // 列選手
                    if (_playerDict.ContainsKey(_selectEventID) && ev.IsTeam == false)
                    {
                        foreach (UDT.Players p in _playerDict[_selectEventID])
                        {
                            int rowIdx = dgData.Rows.Add();
                            dgData.Rows[rowIdx].Cells[colSchoolYear.Index].Value    = ev.SchoolYear;
                            dgData.Rows[rowIdx].Cells[colEventCategory.Index].Value = ev.Category;
                            dgData.Rows[rowIdx].Cells[colEventName.Index].Value     = ev.Name;
                            dgData.Rows[rowIdx].Cells[colGroupType.Index].Value     = gpName;
                            dgData.Rows[rowIdx].Cells[colIsTeam.Index].Value        = ev.IsTeam ? "是" : "否";
                            dgData.Rows[rowIdx].Cells[colTeamName.Index].Value      = "";
                            dgData.Rows[rowIdx].Cells[colPlayer.Index].Value        = p.Name;


                            UDT.HistoricalRecords hr = null;

                            foreach (UDT.HistoricalRecords rec in _HistoricalRecordsList)
                            {
                                // 比對學生系統編號

                                string key = $"\"ref_student_id\":{p.RefStudentId}";

                                if (rec.RefEventId == _selectEventID && rec.Players.Contains(key))
                                {
                                    hr = rec;
                                }
                            }

                            if (hr == null)
                            {
                                hr            = new UDT.HistoricalRecords();
                                hr.RefEventId = _selectEventID;
                                hr.Players    = $"[{parsePlayerToJSonString(p)}]";
                            }

                            if (hr.Rank.HasValue)
                            {
                                dgData.Rows[rowIdx].Cells[colRank.Index].Value = hr.Rank.Value;
                            }

                            dgData.Rows[rowIdx].Tag = hr;

                            rowCount++;
                        }
                    }
                }
            }

            SetDataGridViewCellsReadOnlyColor();

            lblRowCount.Text = $"共{rowCount}筆";
        }