using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;
using System.Resources;

namespace TestGroupingAndPaging
{
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
        }

        private class SpotName
        {
            public String Name = "";
            public int Rank = 0;

            public long WeldCount7 = 0;
            public long FlashWeldCount7 = 0;
            public double PercentOfAllExpulsions7 = 0;
            public double PercentOfAllWelds7 = 0;

            public long WeldCount30 = 0;
            public long FlashWeldCount30 = 0;
            public double PercentOfAllExpulsions30 = 0;
            public double PercentOfAllWelds30 = 0;

            public long WeldCount90 = 0;
            public long FlashWeldCount90 = 0;
            public double PercentOfAllExpulsions90 = 0;
            public double PercentOfAllWelds90 = 0;

            public double FlashPercentage()
            {
                double dblExpulsionPct = 0;
                if (WeldCount7 > 0)
                    dblExpulsionPct = FlashWeldCount7 / WeldCount7;

                return dblExpulsionPct;
            }
        }

        private class SortByFlashPercentage : IComparer<SpotName>
        {
            public int Compare(SpotName x, SpotName y)
            {
                double dblXFlashPercent = x.FlashPercentage();
                double dblYFlashPercent = y.FlashPercentage();
                if (dblYFlashPercent == dblXFlashPercent)
                {
                    return 0;
                }
                else
                {
                    if (dblYFlashPercent > dblXFlashPercent)
                    {
                        return 1;
                    }
                    else
                    {
                        return -1;
                    }
                }
            }
        }



        private System.Collections.Generic.Dictionary<String, SpotName> mcolCustomGroupSpotName = new System.Collections.Generic.Dictionary<String, SpotName>(StringComparer.InvariantCultureIgnoreCase);

        private void RadForm1_Load(object sender, EventArgs e)
        {

        }

        private void gridFlashList_GroupSummaryEvaluate(object sender, GroupSummaryEvaluationEventArgs e)
        {
            if (e.Value == null)
            {
                if (e.Group != null && e.Group.Key != null)
                {
                    e.FormatString = e.Group.Key.ToString();
                }
            }
        }

        private void gridFlashList_CustomGrouping(object sender, GridViewCustomGroupingEventArgs e)
        {
            string strSpotName = e.Row.Cells["SpotName"].Value.ToString();
            string strCollectionCount = this.mcolCustomGroupSpotName.Count.ToString();
            int intDigitCount = strCollectionCount.Length;

            string strRankFormat = "";
            if (intDigitCount > 1)
            {
                strRankFormat = new String('0', intDigitCount - 1);
            }
            strRankFormat = string.Format("{0}#", strRankFormat);
            if (this.mcolCustomGroupSpotName.ContainsKey(strSpotName))
            {
                SpotName objSpotName = mcolCustomGroupSpotName[strSpotName];
                double dblFlashPercentage = objSpotName.FlashPercentage();
                double dblPercentageOfAllFlash = objSpotName.PercentOfAllExpulsions7;
                double dblPercentageOfAllWelds = objSpotName.PercentOfAllWelds7;
                int intRank = objSpotName.Rank;
                string strRank = intRank.ToString(strRankFormat);
                e.GroupKey = String.Format("<html><color=#E0E0E0><b></b>{0}. <color='tan'><b>{1}</b> <color=#6DCCFE>Explusion %: <b>{2}</b> <color='LightGray'></b>",
                                            strRank,
                                            objSpotName.Name,
                                            dblFlashPercentage.ToString("P"));

            }

        }


        #region ViewCellFormatting Methods
        private void gridFlashList_ViewCellFormatting(object sender, CellFormattingEventArgs e)
        {
            e.CellElement.DisableHTMLRendering = false;

            if (sender is Telerik.WinControls.UI.GridDataCellElement)
                GridViewDataCellElementController(sender, e);
            if (sender is Telerik.WinControls.UI.GridHeaderCellElement)
                GridHeaderCellElementController(sender, e);
        }

        public void GridHeaderCellElementController(object sender, CellFormattingEventArgs e)
        {
            Telerik.WinControls.UI.GridHeaderCellElement objElement = (Telerik.WinControls.UI.GridHeaderCellElement)sender;
            System.Data.DataTable dtFlashList = new System.Data.DataTable();
            if (objElement != null)
            {
                Telerik.WinControls.UI.GridViewColumn objColumnInfo = objElement.ColumnInfo;
                if (objElement.ColumnInfo is Telerik.WinControls.UI.GridViewGroupColumn)
                    GridViewGroupColumnController(sender, e);
                else
                {
                    string strColumnText = objElement.Text.ToUpper();
                    string strColumnName = objElement.ColumnInfo.Name.ToUpper();
                    bool blnFormatVertical = false;
                    switch (strColumnName)
                    {
                        case "AREA":
                            {
                                objElement.TextOrientation = Orientation.Vertical;
                                objElement.TextAlignment = ContentAlignment.MiddleLeft;
                                objElement.FlipText = true;
                                e.Column.MinWidth = 52;
                                e.Column.MaxWidth = 52;
                                break;
                            }

                        case "ROBOT":
                            {
                                objElement.TextOrientation = Orientation.Vertical;
                                objElement.TextAlignment = ContentAlignment.MiddleLeft;
                                objElement.FlipText = true;
                                e.Column.MinWidth = 90;
                                e.Column.MaxWidth = 90;
                                break;
                            }

                        case "TYPESERIES":
                            {
                                e.Column.IsVisible = false;
                                blnFormatVertical = true;
                                e.CellElement.TextAlignment = ContentAlignment.BottomCenter;
                                e.Column.MaxWidth = 160;
                                e.Column.MinWidth = 160;
                                break;
                            }

                        case "SPOTNAME" // Contains the highlightable column containing the spot name
                 :
                            {
                                objElement.TextOrientation = Orientation.Horizontal;
                                objElement.TextAlignment = ContentAlignment.BottomLeft;
                                objElement.FlipText = false;
                                e.Column.MinWidth = 100;
                                e.Column.MaxWidth = 100;
                                break;
                            }

                        case "SERIES":
                            {
                                objElement.TextOrientation = Orientation.Vertical;
                                objElement.TextAlignment = ContentAlignment.MiddleLeft;
                                objElement.FlipText = true;
                                e.Column.MinWidth = 40;
                                e.Column.MaxWidth = 40;
                                break;
                            }

                        case "EXT1":
                        case "EXT2":
                        case "EXT3":
                        case "EXT4":
                        case "EXT5":
                        case "EXT6" // Contains the highlightable column containing the spot name
                 :
                            {
                                objElement.TextOrientation = Orientation.Vertical;
                                objElement.TextAlignment = ContentAlignment.MiddleLeft;
                                objElement.FlipText = true;
                                e.Column.MinWidth = 32;
                                e.Column.MaxWidth = 32;
                                break;
                            }

                        case "EXPULSIONS7":
                        case "EXPULSIONS30":
                        case "EXPULSIONS90":
                            {
                                objElement.TextOrientation = Orientation.Vertical;
                                objElement.TextAlignment = ContentAlignment.MiddleLeft;
                                objElement.FlipText = true;
                                e.Column.MinWidth = 50;
                                e.Column.MaxWidth = 50;
                                break;
                            }

                        case "TOTALWELDS7":
                        case "TOTALWELDS30":
                        case "TOTALWELDS90":
                            {
                                objElement.TextOrientation = Orientation.Vertical;
                                objElement.TextAlignment = ContentAlignment.MiddleLeft;
                                objElement.FlipText = true;
                                e.Column.MinWidth = 50;
                                e.Column.MaxWidth = 50;
                                break;
                            }

                        case "EXPULSIONPCT7":
                        case "EXPULSIONPCT30":
                        case "EXPULSIONPCT90":
                            {
                                objElement.TextOrientation = Orientation.Vertical;
                                objElement.TextAlignment = ContentAlignment.MiddleLeft;
                                objElement.FlipText = true;
                                e.Column.MinWidth = 60;
                                e.Column.MaxWidth = 60;
                                break;
                            }

                        case "PCTOFALLEXPULSIONS7":
                        case "PCTOFALLEXPULSIONS30":
                        case "PCTOFALLEXPULSIONS90":
                            {
                                objElement.TextOrientation = Orientation.Vertical;
                                objElement.TextAlignment = ContentAlignment.MiddleLeft;
                                objElement.FlipText = true;
                                e.Column.MinWidth = 60;
                                e.Column.MaxWidth = 60;
                                break;
                            }

                        case "PCTOFALLWELDS7":
                        case "PCTOFALLWELDS30":
                        case "PCTOFALLWELDS90":
                            {
                                objElement.TextOrientation = Orientation.Vertical;
                                objElement.TextAlignment = ContentAlignment.MiddleLeft;
                                objElement.FlipText = true;
                                e.Column.MinWidth = 60;
                                e.Column.MaxWidth = 60;
                                break;
                            }

                        default:
                            {
                                objElement.TextOrientation = Orientation.Horizontal;
                                objElement.TextAlignment = ContentAlignment.BottomLeft;
                                objElement.FlipText = false;
                                e.Column.MinWidth = 60;
                                e.Column.MaxWidth = 80;
                                break;
                            }
                    }
                    if (blnFormatVertical)
                    {
                        e.CellElement.TextOrientation = Orientation.Vertical;
                        e.CellElement.TextAlignment = ContentAlignment.MiddleLeft;
                        e.CellElement.FlipText = true;
                    }
                    e.CellElement.Font = new Font("Segoe UI", 10);
                }
            }
        }
        public void GridViewGroupColumnController(object sender, CellFormattingEventArgs e)
        {
            Telerik.WinControls.UI.GridHeaderCellElement objElement = (Telerik.WinControls.UI.GridHeaderCellElement)sender;
            Telerik.WinControls.UI.GridViewColumn objColumnInfo = objElement.ColumnInfo;
            if (objColumnInfo is Telerik.WinControls.UI.GridViewGroupColumn)
            {
                Telerik.WinControls.UI.GridViewGroupColumn objColumnInfoGroup = (Telerik.WinControls.UI.GridViewGroupColumn)objColumnInfo;
                if (objColumnInfoGroup.Group != null)
                {
                    string strColumnName = objColumnInfoGroup.Name;
                    switch (strColumnName.ToUpper())
                    {
                        case "FLASHLIST":
                            {
                                e.CellElement.Font = new System.Drawing.Font(e.CellElement.Font.FontFamily, 14);
                                e.CellElement.TextAlignment = ContentAlignment.MiddleCenter;
                                break;
                            }

                        case "LAST7DAYS":
                            {
                                e.CellElement.Font = new System.Drawing.Font(e.CellElement.Font.FontFamily, 12);
                                e.CellElement.TextAlignment = ContentAlignment.MiddleCenter;
                                break;
                            }

                        case "LAST30DAYS":
                            {
                                e.CellElement.Font = new System.Drawing.Font(e.CellElement.Font.FontFamily, 12);
                                e.CellElement.TextAlignment = ContentAlignment.MiddleCenter;
                                break;
                            }

                        case "LAST90DAYS":
                            {
                                e.CellElement.Font = new System.Drawing.Font(e.CellElement.Font.FontFamily, 12);
                                e.CellElement.TextAlignment = ContentAlignment.MiddleCenter;
                                break;
                            }
                    }
                }
            }
        }
        public void GridViewDataCellElementController(object sender, CellFormattingEventArgs e)
        {
            Telerik.WinControls.UI.GridDataCellElement objGridDataCellElement = (Telerik.WinControls.UI.GridDataCellElement)sender; // Assign an alias to the GridDataCellElement object
            string strColumnName = objGridDataCellElement.ColumnInfo.FieldName.ToUpper(); // Specifies the column name.
            if (objGridDataCellElement != null)
            {
                // ------------------------------------------------------------------------------
                // Set the PDF icon and color if it is a process number column ("Spotname")
                // ------------------------------------------------------------------------------
                switch (objGridDataCellElement.ColumnInfo.FieldName.ToUpper())
                {
                    case "SPOTNAME":
                        {
                            //this.SpotName_GridViewDataCellElementController(ApplicationSettings, QstopDataGridViewModel, sender, e);
                            break;
                        }

                    default:
                        {
                            if (objGridDataCellElement.Image != null)
                                objGridDataCellElement.Image = null;
                            objGridDataCellElement.ResetValue(Telerik.WinControls.UI.LightVisualElement.ImageProperty, Telerik.WinControls.ValueResetFlags.Local);
                            objGridDataCellElement.ResetValue(Telerik.WinControls.UI.LightVisualElement.ImageAlignmentProperty, Telerik.WinControls.ValueResetFlags.Local);
                            objGridDataCellElement.ResetValue(Telerik.WinControls.UI.LightVisualElement.TextImageRelationProperty, Telerik.WinControls.ValueResetFlags.Local);
                            break;
                        }
                }

                // ------------------------------------------------------------------------------
                // Set the color of the text of the cell.
                // ------------------------------------------------------------------------------
                switch (objGridDataCellElement.ColumnInfo.FieldName.ToUpper())
                {
                    case "SPOTNAME":
                        {
                            break;
                        }

                    case "EXPULSIONS7":
                    case "TOTALWELDS7":
                    case "EXPULSIONPCT7":
                    case "PCTOFALLEXPULSIONS7":
                    case "PCTOFALLWELDS7":
                        {
                            objGridDataCellElement.ForeColor = System.Drawing.Color.FromArgb(109, 204, 254);
                            break;
                        }

                    case "EXPULSIONS30":
                    case "TOTALWELDS30":
                    case "EXPULSIONPCT30":
                    case "PCTOFALLEXPULSIONS30":
                    case "PCTOFALLWELDS30":
                        {
                            objGridDataCellElement.ForeColor = System.Drawing.Color.FromArgb(214, 125, 76);
                            break;
                        }

                    case "EXPULSIONS90":
                    case "TOTALWELDS90":
                    case "EXPULSIONPCT90":
                    case "PCTOFALLEXPULSIONS90":
                    case "PCTOFALLWELDS90":
                        {
                            objGridDataCellElement.ForeColor = System.Drawing.Color.FromArgb(216, 160, 223);
                            break;
                        }

                    default:
                        {
                            objGridDataCellElement.ResetValue(Telerik.WinControls.UI.LightVisualElement.ForeColorProperty);
                            objGridDataCellElement.ResetValue(Telerik.WinControls.UI.LightVisualElement.BackColorProperty);
                            break;
                        }
                }
                // ------------------------------------------------------------------------------
                // Set the alignment of the cell.
                // ------------------------------------------------------------------------------
                switch (objGridDataCellElement.ColumnInfo.FieldName.ToUpper())
                {
                    case "AREA":
                        {
                            e.CellElement.TextAlignment = ContentAlignment.MiddleCenter;
                            break;
                        }

                    case "ROBOT":
                        {
                            e.CellElement.TextAlignment = ContentAlignment.MiddleCenter;
                            break;
                        }

                    case "SERIES":
                        {
                            e.CellElement.TextAlignment = ContentAlignment.MiddleCenter;
                            break;
                        }

                    case "EXT1":
                    case "EXT2":
                    case "EXT3":
                    case "EXT4":
                    case "EXT5":
                    case "EXT6":
                        {
                            e.CellElement.TextAlignment = ContentAlignment.MiddleCenter;
                            break;
                        }

                    case "SPOTNAME":
                        {
                            e.CellElement.TextAlignment = ContentAlignment.MiddleCenter;
                            break;
                        }
                }

                // ------------------------------------------------------------------------------
                // Adjust the double value and make it a percentage.
                // ------------------------------------------------------------------------------
                switch (objGridDataCellElement.ColumnInfo.FieldName.ToUpper())
                {
                    case "EXPULSIONPCT7":
                    case "PCTOFALLEXPULSIONS7":
                    case "PCTOFALLWELDS7":
                    case "EXPULSIONPCT30":
                    case "PCTOFALLEXPULSIONS30":
                    case "PCTOFALLWELDS30":
                    case "EXPULSIONPCT90":
                    case "PCTOFALLEXPULSIONS90":
                    case "PCTOFALLWELDS90":
                        {
                            double dblValue = 0.0;
                            if (double.TryParse(objGridDataCellElement.Value.ToString(), out dblValue))
                                objGridDataCellElement.Text = dblValue.ToString("P");
                            break;
                        }
                }


                if (e.CellElement.Font.Size != 10)
                    e.CellElement.Font = new Font("Segoe UI", 10);
            }
        }
        #endregion

        public void CreateFlashListDataGridView(Telerik.WinControls.UI.RadGridView objGrid)
        {
            Telerik.WinControls.UI.ColumnGroupsViewDefinition objColumnGroupsView = new Telerik.WinControls.UI.ColumnGroupsViewDefinition();
            Telerik.WinControls.UI.GridViewColumnGroup objFakeColumnGroup = new Telerik.WinControls.UI.GridViewColumnGroup("", "FakeColumnGroup");
            objColumnGroupsView.ColumnGroups.Add(objFakeColumnGroup);
            objColumnGroupsView.ColumnGroups[0].Rows.Add(new Telerik.WinControls.UI.GridViewColumnGroupRow());
            objColumnGroupsView.ColumnGroups[0].Rows.Add(new Telerik.WinControls.UI.GridViewColumnGroupRow());
            objFakeColumnGroup.RowSpan = 2;

            Telerik.WinControls.UI.GridViewColumnGroup objFlashInformationColumnGroup = new Telerik.WinControls.UI.GridViewColumnGroup("Flash List", "FlashList");
            objColumnGroupsView.ColumnGroups.Add(objFlashInformationColumnGroup);
            objColumnGroupsView.ColumnGroups[1].Rows.Add(new Telerik.WinControls.UI.GridViewColumnGroupRow());
            objColumnGroupsView.ColumnGroups[1].Rows.Add(new Telerik.WinControls.UI.GridViewColumnGroupRow());
            objFlashInformationColumnGroup.RowSpan = 2;

            Telerik.WinControls.UI.GridViewColumnGroup objRobotAreaSeriesColumnGroup = new Telerik.WinControls.UI.GridViewColumnGroup("", "AreaRobotSpotNameSeriesExtension");
            objColumnGroupsView.ColumnGroups[1].Groups.Add(objRobotAreaSeriesColumnGroup);
            objColumnGroupsView.ColumnGroups[1].Groups[0].Rows.Add(new Telerik.WinControls.UI.GridViewColumnGroupRow());

            Telerik.WinControls.UI.GridViewColumnGroup objPreviousSevenDays = new Telerik.WinControls.UI.GridViewColumnGroup("Last 7 Days", "Last7Days");
            objColumnGroupsView.ColumnGroups[1].Groups.Add(objPreviousSevenDays);
            objColumnGroupsView.ColumnGroups[1].Groups[1].Rows.Add(new Telerik.WinControls.UI.GridViewColumnGroupRow());

            Telerik.WinControls.UI.GridViewColumnGroup objPreviousThirtyDays = new Telerik.WinControls.UI.GridViewColumnGroup("Last 30 Days", "Last30Days");
            objColumnGroupsView.ColumnGroups[1].Groups.Add(objPreviousThirtyDays);
            objColumnGroupsView.ColumnGroups[1].Groups[2].Rows.Add(new Telerik.WinControls.UI.GridViewColumnGroupRow());

            Telerik.WinControls.UI.GridViewColumnGroup objPreviousNinetyDays = new Telerik.WinControls.UI.GridViewColumnGroup("Last 90 Days", "Last90Days");
            objColumnGroupsView.ColumnGroups[1].Groups.Add(objPreviousNinetyDays);
            objColumnGroupsView.ColumnGroups[1].Groups[3].Rows.Add(new Telerik.WinControls.UI.GridViewColumnGroupRow());


            // --------------------------------------------------------------------------------------
            // Add a GridViewSummaryRowItem row to the bottom of the grid if it has not been added.
            // --------------------------------------------------------------------------------------
            objColumnGroupsView.ColumnGroups[1].Groups[0].Rows[0].ColumnNames.Add("Area");
            objColumnGroupsView.ColumnGroups[1].Groups[0].Rows[0].ColumnNames.Add("Robot");
            objColumnGroupsView.ColumnGroups[1].Groups[0].Rows[0].ColumnNames.Add("_SpotName");
            objColumnGroupsView.ColumnGroups[1].Groups[0].Rows[0].ColumnNames.Add("SpotName");
            objColumnGroupsView.ColumnGroups[1].Groups[0].Rows[0].ColumnNames.Add("TypeSeries");
            objColumnGroupsView.ColumnGroups[1].Groups[0].Rows[0].ColumnNames.Add("Series");
            objColumnGroupsView.ColumnGroups[1].Groups[0].Rows[0].ColumnNames.Add("Ext1");
            objColumnGroupsView.ColumnGroups[1].Groups[0].Rows[0].ColumnNames.Add("Ext2");
            objColumnGroupsView.ColumnGroups[1].Groups[0].Rows[0].ColumnNames.Add("Ext3");
            objColumnGroupsView.ColumnGroups[1].Groups[0].Rows[0].ColumnNames.Add("Ext4");
            objColumnGroupsView.ColumnGroups[1].Groups[0].Rows[0].ColumnNames.Add("Ext5");
            objColumnGroupsView.ColumnGroups[1].Groups[0].Rows[0].ColumnNames.Add("Ext6");
            objColumnGroupsView.ColumnGroups[1].Groups[1].Rows[0].ColumnNames.Add("Expulsions7");
            objColumnGroupsView.ColumnGroups[1].Groups[1].Rows[0].ColumnNames.Add("TotalWelds7");
            objColumnGroupsView.ColumnGroups[1].Groups[1].Rows[0].ColumnNames.Add("ExpulsionPct7");
            objColumnGroupsView.ColumnGroups[1].Groups[1].Rows[0].ColumnNames.Add("PctOfAllWelds7");
            objColumnGroupsView.ColumnGroups[1].Groups[1].Rows[0].ColumnNames.Add("PctOfAllExpulsions7");
            objColumnGroupsView.ColumnGroups[1].Groups[2].Rows[0].ColumnNames.Add("Expulsions30");
            objColumnGroupsView.ColumnGroups[1].Groups[2].Rows[0].ColumnNames.Add("TotalWelds30");
            objColumnGroupsView.ColumnGroups[1].Groups[2].Rows[0].ColumnNames.Add("ExpulsionPct30");
            objColumnGroupsView.ColumnGroups[1].Groups[2].Rows[0].ColumnNames.Add("PctOfAllWelds30");
            objColumnGroupsView.ColumnGroups[1].Groups[2].Rows[0].ColumnNames.Add("PctOfAllExpulsions30");
            objColumnGroupsView.ColumnGroups[1].Groups[3].Rows[0].ColumnNames.Add("Expulsions90");
            objColumnGroupsView.ColumnGroups[1].Groups[3].Rows[0].ColumnNames.Add("TotalWelds90");
            objColumnGroupsView.ColumnGroups[1].Groups[3].Rows[0].ColumnNames.Add("ExpulsionPct90");
            objColumnGroupsView.ColumnGroups[1].Groups[3].Rows[0].ColumnNames.Add("PctOfAllWelds90");
            objColumnGroupsView.ColumnGroups[1].Groups[3].Rows[0].ColumnNames.Add("PctOfAllExpulsions90");
            objGrid.ViewDefinition = objColumnGroupsView;
            objGrid.TableElement.CellSpacing = 1;
            objGrid.TableElement.RowSpacing = 1;
        }

        private void btnLoadData_Click(object sender, EventArgs e)
        {
            System.Data.DataTable dtData = new System.Data.DataTable();
            System.Data.DataTable FlashListBySpotNameData = new System.Data.DataTable();
            BinaryFormatter objBinFormatter = new BinaryFormatter();
            this.gridFlashList.MasterTemplate.AutoExpandGroups = true;

            gridFlashList.DataSource = null;

            //------------------------------------------------------------------------------
            //Setup the grid sort descriptors.
            //------------------------------------------------------------------------------
            this.gridFlashList.SortDescriptors.Clear(); //'Clear the SortDescriptors
            if (this.tsSortDescriptors.Value)
            {
                Telerik.WinControls.Data.SortDescriptor objExpulsionPctSortDescriptor = new Telerik.WinControls.Data.SortDescriptor();
                objExpulsionPctSortDescriptor.PropertyName = "ExpulsionPct7";
                objExpulsionPctSortDescriptor.Direction = System.ComponentModel.ListSortDirection.Descending;
                gridFlashList.SortDescriptors.Add(objExpulsionPctSortDescriptor);
                gridFlashList.ColumnChooserSortOrder = Telerik.WinControls.UI.RadSortOrder.Descending;

            }


            if (rdoSmallerDataTable.IsChecked )
            {
                //------------------------------------------------------------------------------
                //Deserialize the binary file in the resources containing the data.
                //------------------------------------------------------------------------------
                using (System.IO.MemoryStream objMS = new System.IO.MemoryStream(TestGroupingAndPaging.Properties.Resources.UIFlashListDataTable))
                {
                    dtData = (System.Data.DataTable)objBinFormatter.Deserialize(objMS);
                }
                //------------------------------------------------------------------------------
                //Deserialize the binary file containing the aggregated counts of the Flash List
                //------------------------------------------------------------------------------
                using (System.IO.MemoryStream objMS = new System.IO.MemoryStream(TestGroupingAndPaging.Properties.Resources.FlashListBySpotName))
                {
                    FlashListBySpotNameData = (System.Data.DataTable)objBinFormatter.Deserialize(objMS);
                }

            }

            if (rdoLargerDataTable.IsChecked)
            {
                //------------------------------------------------------------------------------
                //Deserialize the binary file in the resources containing the data.
                //------------------------------------------------------------------------------
                using (System.IO.MemoryStream objMS = new System.IO.MemoryStream(TestGroupingAndPaging.Properties.Resources.LargeUIFlashListDataTable))
                {
                    dtData = (System.Data.DataTable)objBinFormatter.Deserialize(objMS);
                }
                //------------------------------------------------------------------------------
                //Deserialize the binary file containing the aggregated counts of the Flash List
                //------------------------------------------------------------------------------
                using (System.IO.MemoryStream objMS = new System.IO.MemoryStream(TestGroupingAndPaging.Properties.Resources.LargeFlashListBySpotName))
                {
                    FlashListBySpotNameData = (System.Data.DataTable)objBinFormatter.Deserialize(objMS);
                }
            }


            //------------------------------------------------------------------------------
            //Setup the group descriptors 
            //------------------------------------------------------------------------------

            if (this.gridFlashList.GroupDescriptors.Count == 0)
            {
                Telerik.WinControls.Data.GroupDescriptor objSpotNameDescriptor = new Telerik.WinControls.Data.GroupDescriptor("_SpotName");
                this.gridFlashList.GroupDescriptors.Add(objSpotNameDescriptor);
            }

            //------------------------------------------------------------------------------
            //Setup the VScrollBar
            //------------------------------------------------------------------------------
            this.gridFlashList.GridViewElement.TableElement.VScrollBar.MinSize = new Size(30, 0);
            this.gridFlashList.GridViewElement.TableElement.VScrollBar.ThumbElement.MinSize = new Size(20, 0);
            this.gridFlashList.GridViewElement.TableElement.VScrollBar.ThumbElement.FitToSizeMode = Telerik.WinControls.RadFitToSizeMode.FitToParentBounds;

            //------------------------------------------------------------------------------
            //Iterate through the FlashListBySpotNameData datatable to build the table 
            //------------------------------------------------------------------------------
            foreach (System.Data.DataRow objDR in FlashListBySpotNameData.Rows)
            {
                String strSpotName = (string)objDR["SpotName"].ToString();

                long lngFlash7 = (long)objDR["FlashWelds7"];
                long lngWelds7 = (long)objDR["TotalWelds7"];
                if (!mcolCustomGroupSpotName.ContainsKey(strSpotName))
                {
                    SpotName objSpotName = new SpotName();
                    objSpotName.Name = strSpotName;
                    objSpotName.WeldCount7 = lngWelds7;
                    objSpotName.FlashWeldCount7 = lngFlash7;
                    mcolCustomGroupSpotName.Add(strSpotName, objSpotName);
                }
            }

            //------------------------------------------------------------------------------
            //Add the Spotname flash percentage to a sorted list so we can assign 
            //the ranking.
            //------------------------------------------------------------------------------
            List<SpotName> colFlashPercentage = new List<SpotName>();
            foreach (System.Collections.Generic.KeyValuePair<String, SpotName> objKVP in mcolCustomGroupSpotName)
            {
                colFlashPercentage.Add(objKVP.Value);
            }
            //------------------------------------------------------------------------------
            //Sort the list based on the flash percentage.
            //------------------------------------------------------------------------------
            colFlashPercentage.Sort(new SortByFlashPercentage());

            //------------------------------------------------------------------------------
            //Assign the rank to each item.
            //------------------------------------------------------------------------------
            int intRank = 0;
            if (colFlashPercentage.Count > 0)
            {
                foreach (SpotName objSpotName in colFlashPercentage)

                {
                    if (mcolCustomGroupSpotName.ContainsKey(objSpotName.Name))
                    {
                        intRank += 1;
                        mcolCustomGroupSpotName[objSpotName.Name].Rank = intRank;
                    }
                }
            }

            this.CreateFlashListDataGridView(this.gridFlashList);


            //------------------------------------------------------------------------------
            //Set the grid's datasource
            //------------------------------------------------------------------------------
            this.gridFlashList.BeginUpdate();
            this.gridFlashList.DataSource = dtData;
            this.gridFlashList.EndUpdate();
            this.gridFlashList.MasterTemplate.ExpandAll();

        }
    }
}
