private void gridMain_OnSortByColumn(object sender, EventArgs e)
 {
     for (int i = 0; i < gridMain.ListGridRows.Count; i++)
     {
         LimitedRow lRow = gridMain.ListGridRows[i].Tag as LimitedRow;
         gridMain.SetSelected(i, _dictSelectedRows.TryGetValue(lRow.Type, out List <long> listPriKeys) && listPriKeys.Contains(lRow.PrimaryKey));
     }
 }
 private void ConstructListFromTable()
 {
     foreach (DataRow tableRow in _tableAccount.Rows)
     {
         LimitedRow row = new LimitedRow();
         row.PatNum      = PIn.Long(tableRow["PatNum"].ToString());
         row.PatientName = tableRow["patient"].ToString();
         row.DateTime    = PIn.DateT(tableRow["DateTime"].ToString());
         row.Description = tableRow["description"].ToString();
         row.ProcCode    = tableRow["ProcCode"].ToString();           //isn't just a proc code. Can be "Claim" etc...
         row.Charges     = tableRow["charges"].ToString();
         row.Credits     = tableRow["credits"].ToString();
         row.ProvName    = tableRow["prov"].ToString();
         row.Tooth       = Tooth.ToInternat(tableRow["ToothNum"].ToString());
         row.ColorText   = Color.FromArgb(PIn.Int(tableRow["colorText"].ToString()));
         if (PrefC.HasClinicsEnabled)
         {
             row.ClinicNum = PIn.Long(tableRow["ClinicNum"].ToString());
         }
         if (tableRow["AdjNum"].ToString() != "0")
         {
             row.PrimaryKey = PIn.Long(tableRow["AdjNum"].ToString());
             row.Type       = AccountEntryType.Adjustment;
         }
         else if (tableRow["ProcNum"].ToString() != "0")
         {
             row.PrimaryKey = PIn.Long(tableRow["ProcNum"].ToString());
             row.Type       = AccountEntryType.Procedure;
         }
         else if (tableRow["PayNum"].ToString() != "0")
         {
             row.PrimaryKey = PIn.Long(tableRow["PayNum"].ToString());
             row.Type       = AccountEntryType.Payment;
         }
         else if (tableRow["ClaimNum"].ToString() != "0")
         {
             //can mean that this is either a claim or a claim payment.
             //we really only care about claim payments, but we need procedure from the claim.
             row.PrimaryKey = PIn.Long(tableRow["ClaimNum"].ToString());
             row.Type       = AccountEntryType.Claim;
             if (tableRow["ClaimPaymentNum"].ToString() == "1")
             {
                 row.Type = AccountEntryType.ClaimPayment;
             }
         }
         else
         {
             //type is not one that is currently supported, skip it.
             continue;
         }
         row.ListProcsOnObject = tableRow["procsOnObj"].ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => PIn.Long(x)).ToList();
         row.ListAdjustsOnObj  = tableRow["adjustsOnObj"].ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => PIn.Long(x)).ToList();
         row.ListPaymentsOnObj = tableRow["paymentsOnObj"].ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => PIn.Long(x)).ToList();
         row.ProcNumLab        = PIn.Long(tableRow["ProcNumLab"].ToString());
         _listLimitedRows.Add(row);
     }
 }
        private void SelectAssociatedTrans()
        {
            List <LimitedRow> listSelectedRows         = gridMain.SelectedTags <LimitedRow>();
            List <long>       listSelectedPayClaimNums = listSelectedRows.FindAll(x => x.Type == AccountEntryType.ClaimPayment).Select(x => x.PrimaryKey).Distinct().ToList();
            List <long>       listSelectedProcNums     = listSelectedRows.FindAll(x => x.Type == AccountEntryType.Procedure).Select(x => x.PrimaryKey).Distinct().ToList();
            List <long>       listSelectedPayNums      = listSelectedRows.FindAll(x => x.Type == AccountEntryType.Payment).Select(x => x.PrimaryKey).Distinct().ToList();
            List <long>       listSelectedAdjNums      = listSelectedRows.FindAll(x => x.Type == AccountEntryType.Adjustment).Select(x => x.PrimaryKey).Distinct().ToList();
            List <int>        listSelectedIndices      = gridMain.SelectedIndices.ToList();

            for (int i = 0; i < listSelectedIndices.Count; i++)
            {
                LimitedRow lSelectedRow = gridMain.ListGridRows[listSelectedIndices[i]].Tag as LimitedRow;
                if (lSelectedRow.Type == AccountEntryType.ClaimPayment)
                {
                    listSelectedProcNums.AddRange(lSelectedRow.ListProcsOnObject.Where(x => x > 0 && !listSelectedProcNums.Contains(x)));
                    List <LimitedRow> listClaimRows = _listLimitedRows.FindAll(x => x.Type == AccountEntryType.Claim && x.PrimaryKey.In(listSelectedPayClaimNums));
                    listSelectedProcNums.AddRange(listClaimRows.SelectMany(x => x.ListProcsOnObject.Where(y => y > 0 && !listSelectedProcNums.Contains(y))).Distinct());
                    for (int j = 0; j < gridMain.ListGridRows.Count; j++)
                    {
                        LimitedRow lRow = gridMain.ListGridRows[j].Tag as LimitedRow;
                        if (lRow.Type == AccountEntryType.ClaimPayment && lRow.PrimaryKey.In(listSelectedPayClaimNums))
                        {
                            if (!listSelectedIndices.Contains(j))
                            {
                                listSelectedIndices.Add(j);
                                gridMain.SetSelected(j, true);
                            }
                            listSelectedProcNums.AddRange(lRow.ListProcsOnObject.Where(x => x > 0 && !listSelectedProcNums.Contains(x)).Distinct());
                        }
                    }
                    listSelectedProcNums = listSelectedProcNums.Distinct().Where(x => x > 0).ToList();
                    for (int j = 0; j < gridMain.ListGridRows.Count; j++)
                    {
                        LimitedRow lRow = gridMain.ListGridRows[j].Tag as LimitedRow;
                        if (lRow.Type == AccountEntryType.Procedure && listSelectedProcNums.Any(x => lRow.PrimaryKey == x || lRow.ProcNumLab == x))
                        {
                            if (!listSelectedIndices.Contains(j))
                            {
                                listSelectedIndices.Add(j);
                                gridMain.SetSelected(j, true);
                            }
                        }
                    }
                }
                else if (lSelectedRow.Type == AccountEntryType.Payment)
                {
                    listSelectedProcNums.AddRange(lSelectedRow.ListProcsOnObject.Where(x => x > 0 && !listSelectedProcNums.Contains(x)));
                    listSelectedPayNums.AddRange(lSelectedRow.ListPaymentsOnObj.Where(x => x > 0 && !listSelectedPayNums.Contains(x)));
                    listSelectedAdjNums.AddRange(lSelectedRow.ListAdjustsOnObj.Where(x => x > 0 && !listSelectedAdjNums.Contains(x)));
                    for (int j = 0; j < gridMain.ListGridRows.Count; j++)
                    {
                        LimitedRow lRow = gridMain.ListGridRows[j].Tag as LimitedRow;
                        if (lRow.Type == AccountEntryType.Payment && listSelectedPayNums.Contains(lRow.PrimaryKey))
                        {
                            if (!listSelectedIndices.Contains(j))
                            {
                                listSelectedIndices.Add(j);
                                gridMain.SetSelected(j, true);
                            }
                            listSelectedProcNums.AddRange(lRow.ListProcsOnObject.Where(x => x > 0 && !listSelectedProcNums.Contains(x)));
                            listSelectedPayNums.AddRange(lRow.ListPaymentsOnObj.Where(x => x > 0 && !listSelectedPayNums.Contains(x)));
                            listSelectedAdjNums.AddRange(lRow.ListAdjustsOnObj.Where(x => x > 0 && !listSelectedAdjNums.Contains(x)));
                        }
                    }
                    for (int j = 0; j < gridMain.ListGridRows.Count; j++)
                    {
                        LimitedRow lRow = gridMain.ListGridRows[j].Tag as LimitedRow;
                        if (lRow.Type == AccountEntryType.Procedure && listSelectedProcNums.Contains(lRow.PrimaryKey))
                        {
                            if (!listSelectedIndices.Contains(j))
                            {
                                listSelectedIndices.Add(j);
                                gridMain.SetSelected(j, true);
                            }
                        }
                        else if (lRow.Type == AccountEntryType.Payment && listSelectedPayNums.Contains(lRow.PrimaryKey))
                        {
                            if (!listSelectedIndices.Contains(j))
                            {
                                listSelectedIndices.Add(j);
                                gridMain.SetSelected(j, true);
                            }
                        }
                        else if (lRow.Type == AccountEntryType.Adjustment && listSelectedAdjNums.Contains(lRow.PrimaryKey))
                        {
                            if (!listSelectedIndices.Contains(j))
                            {
                                listSelectedIndices.Add(j);
                                gridMain.SetSelected(j, true);
                            }
                        }
                    }
                }
                else if (lSelectedRow.Type == AccountEntryType.Adjustment && lSelectedRow.ListProcsOnObject.Count == 1)
                {
                    for (int j = 0; j < gridMain.ListGridRows.Count; j++)
                    {
                        LimitedRow lRow = gridMain.ListGridRows[j].Tag as LimitedRow;
                        if (lRow.Type == AccountEntryType.Procedure && lRow.PrimaryKey == lSelectedRow.ListProcsOnObject[0])
                        {
                            if (!listSelectedIndices.Contains(j))
                            {
                                listSelectedIndices.Add(j);
                                gridMain.SetSelected(j, true);
                            }
                            break;
                        }
                    }
                }
                else if (lSelectedRow.Type == AccountEntryType.Procedure || lSelectedRow.ProcNumLab != 0)
                {
                    for (int j = 0; j < gridMain.ListGridRows.Count; j++)
                    {
                        LimitedRow lRow = gridMain.ListGridRows[j].Tag as LimitedRow;
                        if (lSelectedRow.ProcNumLab > 0)
                        {
                            if (lRow.PrimaryKey == lSelectedRow.ProcNumLab || lRow.ProcNumLab == lSelectedRow.ProcNumLab)
                            {
                                if (!listSelectedIndices.Contains(j))
                                {
                                    listSelectedIndices.Add(j);
                                    gridMain.SetSelected(j, true);
                                }
                            }
                        }
                        else if (lSelectedRow.PrimaryKey == lRow.ProcNumLab)
                        {
                            if (!listSelectedIndices.Contains(j))
                            {
                                listSelectedIndices.Add(j);
                                gridMain.SetSelected(j, true);
                            }
                        }
                    }
                }
            }
            _dictSelectedRows = gridMain.SelectedTags <LimitedRow>()
                                .GroupBy(x => x.Type, x => x.PrimaryKey)
                                .ToDictionary(x => x.Key, x => x.Distinct().ToList());
        }
        private void FillGrid()
        {
            gridMain.BeginUpdate();
            gridMain.ListGridColumns.Clear();
            List <DisplayField> fieldsForGrid = DisplayFields.GetForCategory(DisplayFieldCategory.AccountModule);

            if (!PrefC.HasClinicsEnabled)
            {
                //remove clinics from displayfields if clinics are disabled
                fieldsForGrid.RemoveAll(x => x.InternalName.ToLower().Contains("clinic"));
            }
            fieldsForGrid.RemoveAll(x => x.InternalName.In("Abbr", "Balance", "Signed"));
            HorizontalAlignment align;
            GridSortingStrategy sort;

            for (int i = 0; i < fieldsForGrid.Count; i++)
            {
                align = HorizontalAlignment.Left;
                sort  = GridSortingStrategy.StringCompare;
                if (fieldsForGrid[i].InternalName.In("Charges", "Credits"))
                {
                    align = HorizontalAlignment.Right;
                    sort  = GridSortingStrategy.AmountParse;
                }
                if (fieldsForGrid[i].InternalName == "Tth")
                {
                    sort = GridSortingStrategy.ToothNumberParse;
                }
                if (fieldsForGrid[i].InternalName == "Date")
                {
                    sort = GridSortingStrategy.DateParse;
                }
                gridMain.ListGridColumns.Add(new GridColumn(fieldsForGrid[i].Description == ""?fieldsForGrid[i].InternalName:fieldsForGrid[i].Description,
                                                            fieldsForGrid[i].ColumnWidth, align, sort));
            }
            if (gridMain.ListGridColumns.Sum(x => x.ColWidth) > gridMain.Width)
            {
                gridMain.HScrollVisible = true;
            }
            gridMain.ListGridRows.Clear();
            GridRow row;

            for (int i = 0; i < _listLimitedRows.Count; i++)
            {
                LimitedRow limitedRow = _listLimitedRows[i];
                if (!limitedRow.Type.In(listBoxTransTypes.GetListSelected <AccountEntryType>()))
                {
                    continue;
                }
                DateTime date = limitedRow.DateTime.Date;
                if (date.Date < odDatePickerFrom.GetDateTime().Date || date.Date > odDatePickerTo.GetDateTime().Date)
                {
                    continue;                    //do not add to grid if it is outside the filtered date range.
                }
                row = new GridRow();
                for (int f = 0; f < fieldsForGrid.Count; f++)
                {
                    switch (fieldsForGrid[f].InternalName)
                    {
                    case "Date":
                        row.Cells.Add(date.ToShortDateString());
                        break;

                    case "Patient":
                        row.Cells.Add(limitedRow.PatientName);
                        break;

                    case "Prov":
                        row.Cells.Add(limitedRow.ProvName);
                        break;

                    case "Clinic":
                        row.Cells.Add(Clinics.GetAbbr(limitedRow.ClinicNum));
                        break;

                    case "ClinicDesc":
                        row.Cells.Add(Clinics.GetDesc(limitedRow.ClinicNum));
                        break;

                    case "Code":
                        row.Cells.Add(limitedRow.ProcCode);
                        break;

                    case "Tth":
                        row.Cells.Add(limitedRow.Tooth);
                        break;

                    case "Description":
                        row.Cells.Add(limitedRow.Description);
                        break;

                    case "Charges":
                        row.Cells.Add(limitedRow.Charges);
                        break;

                    case "Credits":
                        row.Cells.Add(limitedRow.Credits);
                        break;

                    default:
                        row.Cells.Add("");
                        break;
                    }
                }
                row.ColorText = limitedRow.ColorText;
                if (i == _listLimitedRows.Count - 1 || limitedRow.DateTime.Date != _listLimitedRows[i + 1].DateTime.Date)
                {
                    row.ColorLborder = Color.Black;
                }
                row.Tag = limitedRow;
                gridMain.ListGridRows.Add(row);
            }
            gridMain.EndUpdate();
            for (int i = 0; i < gridMain.ListGridRows.Count; i++)
            {
                LimitedRow lRow = gridMain.ListGridRows[i].Tag as LimitedRow;
                gridMain.SetSelected(i, _dictSelectedRows.TryGetValue(lRow.Type, out List <long> listPriKeys) && listPriKeys.Contains(lRow.PrimaryKey));
            }
            //this will refresh _dictSelectedRows and add any associated trans to the previously selected rows
            SelectAssociatedTrans();
        }