public static UploadFormState RecordState(UploadForm mFrm)
        {
            var mJson = new JavaScriptSerializer();
            var mState = new UploadFormState();

            mState.StatVarCol1 = (string)mFrm.cbColCType1.SelectedValue;
            mState.StatVarCol2 = (string)mFrm.cbColCType2.SelectedValue;
            mState.StatVarCol3 = (string)mFrm.cbColCType3.SelectedValue;
            mState.StatVarCol4 = (string)mFrm.cbColCType4.SelectedValue;

            mState.StatVarRow1 = (string)mFrm.cbRowCType1.SelectedValue;
            mState.StatVarRow2 = (string)mFrm.cbRowCType2.SelectedValue;
            mState.StatVarRow3 = (string)mFrm.cbRowCType3.SelectedValue;
            mState.StatVarRow4 = (string)mFrm.cbRowCType4.SelectedValue;

            mState.StatDatumFormat = (string)mFrm.cbStatDatumFormat.SelectedValue;
            mState.StatDatumYear = (string)mFrm.tbStatDatumYear.Text;
            mState.StatDatumQuarter = (string)mFrm.cbStatDatumQuarter.Text;
            mState.StatDatumMonth = (string)mFrm.tbStatDatumMonth.Text;

            mState.StatUnitType = (string)mFrm.cbStatUnitType.SelectedValue;
            mState.StatUnitID = (string)mFrm.tbStatUnitID.Text;
            mState.StatUnitName = (string)mFrm.tbStatUnitName.Text;
            mState.StatUnitGroup = (string)mFrm.tbStatUnitGroup.Text;

            mState.StatVarProperties = UploadFormFieldState.GetFieldStates(mFrm.dgvStatVarProperties);

            return mState;
        }
        private void btnUpload_Click(object sender, RibbonControlEventArgs e)
        {
            var mSelection = this.GetSelectedRange();

            if (mSelection.Cells.Value is string || mSelection.Cells.Value == null || (mSelection.Cells.Width < 2 && mSelection.Cells.Height < 2))
            {
                return;
            }

            var mFrm = new UploadForm();
            mFrm.SelectedRange = this.GetSelectedRange();
            mFrm.ShowDialog();

        }
        public static bool RestoreState(UploadForm mFrm, UploadFormState mState)
        {

            UploadFormState.RestoreCB(mFrm.cbColCType1, mState.StatVarCol1);
            mFrm.OnCellContenTypeComboBoxChangeCommit(mFrm.cbColCType1, DataOrientation.InColumns, 1);
            UploadFormState.RestoreCB(mFrm.cbColCType2, mState.StatVarCol2);
            mFrm.OnCellContenTypeComboBoxChangeCommit(mFrm.cbColCType2, DataOrientation.InColumns, 2);
            UploadFormState.RestoreCB(mFrm.cbColCType3, mState.StatVarCol3);
            mFrm.OnCellContenTypeComboBoxChangeCommit(mFrm.cbColCType3, DataOrientation.InColumns, 3);
            UploadFormState.RestoreCB(mFrm.cbColCType4, mState.StatVarCol4);
            mFrm.OnCellContenTypeComboBoxChangeCommit(mFrm.cbColCType4, DataOrientation.InColumns, 4);

            UploadFormState.RestoreCB(mFrm.cbRowCType1, mState.StatVarRow1);
            mFrm.OnCellContenTypeComboBoxChangeCommit(mFrm.cbRowCType1, DataOrientation.InRows, 1);
            UploadFormState.RestoreCB(mFrm.cbRowCType2, mState.StatVarRow2);
            mFrm.OnCellContenTypeComboBoxChangeCommit(mFrm.cbRowCType2, DataOrientation.InRows, 2);
            UploadFormState.RestoreCB(mFrm.cbRowCType3, mState.StatVarRow3);
            mFrm.OnCellContenTypeComboBoxChangeCommit(mFrm.cbRowCType3, DataOrientation.InRows, 3);
            UploadFormState.RestoreCB(mFrm.cbRowCType4, mState.StatVarRow4);
            mFrm.OnCellContenTypeComboBoxChangeCommit(mFrm.cbRowCType4, DataOrientation.InRows, 4);

            UploadFormState.RestoreCB(mFrm.cbStatUnitType, mState.StatUnitType);
            UploadFormState.RestoreTB(mFrm.tbStatUnitID, mState.StatUnitID);

            if (mState.StatUnitName != null)
            {
                mFrm.tbStatUnitName.Text = mState.StatUnitName;
            }
            if (mState.StatUnitGroup != null)
            {
                mFrm.tbStatUnitGroup.Text = mState.StatUnitGroup;
            }

            if (mState.StatDatumFormat != null)
            {
                mFrm.cbStatDatumFormat.SelectedValue = mState.StatDatumFormat;
            }
            if (mState.StatDatumYear != null)
            {
                mFrm.tbStatDatumYear.Text = mState.StatDatumYear;
            }
            if (mState.StatDatumQuarter != null)
            {
                mFrm.cbStatDatumQuarter.Text = mState.StatDatumQuarter;
            }
            if (mState.StatDatumMonth != null)
            {
                mFrm.tbStatDatumMonth.Text = mState.StatDatumMonth;
            }

            mFrm.LoadStatVarPropertiesGrid();

            var mNumStatVarProperties = mFrm.dgvStatVarProperties.Rows.Count;
            var mNumStoredProperties = mState.StatVarProperties.Count();
            var mLimit = (mNumStoredProperties < mNumStatVarProperties) ? mNumStoredProperties : mNumStatVarProperties;

            for (int i = 0; i < mLimit; i++)
            {
                // Get current stored field
                var mStatVarProperty = mState.StatVarProperties[i];

                // Get corresponding row in datagridview
                DataGridViewCellCollection mRow = mFrm.dgvStatVarProperties.Rows[i].Cells;

                // Create a variable for each of the combobox cells
                DataGridViewComboBoxCell mStatVarCol1 = (DataGridViewComboBoxCell)mRow["StatVarCol1"];
                DataGridViewComboBoxCell mStatVarCol2 = (DataGridViewComboBoxCell)mRow["StatVarCol2"];
                DataGridViewComboBoxCell mStatVarCol3 = (DataGridViewComboBoxCell)mRow["StatVarCol3"];
                DataGridViewComboBoxCell mStatVarCol4 = (DataGridViewComboBoxCell)mRow["StatVarCol4"];
                DataGridViewComboBoxCell mStatVarCol5 = (DataGridViewComboBoxCell)mRow["StatVarCol5"];
                DataGridViewComboBoxCell mMeasurementUnitCol = (DataGridViewComboBoxCell)mRow["enhet"];

                // Add the selected item to the combobox cells
                if (mStatVarProperty.StatVarLevel1 != null)
                {
                    mStatVarCol1.Items.Add(ComboBoxItem.GetNewItem(mStatVarProperty.StatVarLevel1));
                    mStatVarCol1.ValueMember = "value";
                    mStatVarCol1.DisplayMember = "key";
                    mStatVarCol1.Value = mStatVarProperty.StatVarLevel1;
                }
                if (mStatVarProperty.StatVarLevel2 != null)
                {
                    mStatVarCol2.Items.Add(ComboBoxItem.GetNewItem(mStatVarProperty.StatVarLevel2));
                    mStatVarCol2.ValueMember = "value";
                    mStatVarCol2.DisplayMember = "key";
                    mStatVarCol2.Value = mStatVarProperty.StatVarLevel2;
                }
                if (mStatVarProperty.StatVarLevel3 != null)
                {
                    mStatVarCol3.Items.Add(ComboBoxItem.GetNewItem(mStatVarProperty.StatVarLevel3));
                    mStatVarCol3.ValueMember = "value";
                    mStatVarCol3.DisplayMember = "key";
                    mStatVarCol3.Value = mStatVarProperty.StatVarLevel3;
                }
                if (mStatVarProperty.StatVarLevel4 != null)
                {
                    mStatVarCol4.Items.Add(ComboBoxItem.GetNewItem(mStatVarProperty.StatVarLevel4));
                    mStatVarCol4.ValueMember = "value";
                    mStatVarCol4.DisplayMember = "key";
                    mStatVarCol4.Value = mStatVarProperty.StatVarLevel4;
                }
                if (mStatVarProperty.StatVarLevel5 != null)
                {
                    mStatVarCol5.Items.Add(ComboBoxItem.GetNewItem(mStatVarProperty.StatVarLevel5));
                    mStatVarCol5.ValueMember = "value";
                    mStatVarCol5.DisplayMember = "key";
                    mStatVarCol5.Value = mStatVarProperty.StatVarLevel5;
                }
                if (mStatVarProperty.MeasurementUnit != null)
                {
                    mMeasurementUnitCol.Items.Add(ComboBoxItem.GetNewItem(mStatVarProperty.MeasurementUnit));
                    mStatVarCol5.ValueMember = "value";
                    mStatVarCol5.DisplayMember = "key";
                    mMeasurementUnitCol.Value = mStatVarProperty.MeasurementUnit;
                }

                mRow["ar"].Value = mStatVarProperty.Year;
                mRow["kvartal"].Value = mStatVarProperty.Quarter;
                mRow["mnd"].Value = mStatVarProperty.Month;
                mRow["Day"].Value = mStatVarProperty.Day;

            }
            return true;
        }
        public static Values3D ParseSelectionWithCurrentSettings(Range pSelection, UploadForm pFrm, int mFirstDataRow, int mFirstDataCol)
        {

            var mMessages = new MessageObject();

            pFrm.Log("Freistar å prosessere utvalet med gjeldande innstillingar", true);

            var mTimer = new Stopwatch();
            mTimer.Start();

            var mSelection = pSelection.Cells.Value;

            // Create data object to hold values
            var mData = new Values3D();

            // For each row
            for (int mR = mFirstDataRow; mR <= pSelection.Rows.Count; mR++)
            {
                // For each column
                for (int mC = mFirstDataCol; mC <= pSelection.Columns.Count; mC++)
                {
                    // Get the value (as a double)
                    var mAdaptiveValue = new AdaptiveValue(Table.GetNullOrDoubleString(mSelection[mR, mC]));

                    // Get manual settings for statistical variable
                    var mManualStatVarProps = pFrm.ParseStatVarProperties(mR, mC, pFrm.StatVarProperties.DataOrientation);

                    // Get/set the corresponding statvar
                    if (mManualStatVarProps.StatVar1 != null)
                    {
                        mAdaptiveValue.variable1 = mManualStatVarProps.StatVar1.var1;

                        if (String.IsNullOrWhiteSpace(mManualStatVarProps.StatVar1.var1))
                        {
                            mMessages.AddMessage(MessagesNotices.StatVarParseUsingDefaultNotice);
                        }
                        
                    }

                    if (mManualStatVarProps.StatVar2 != null)
                    {
                        mAdaptiveValue.variable2 = mManualStatVarProps.StatVar2.var2;
                    }

                    if (mManualStatVarProps.StatVar3 != null)
                    {
                        mAdaptiveValue.variable3 = mManualStatVarProps.StatVar3.var3;
                    }

                    if (mManualStatVarProps.StatVar4 != null)
                    {
                        mAdaptiveValue.variable4 = mManualStatVarProps.StatVar4.var4;
                    }

                    if (mManualStatVarProps.StatVar5 != null)
                    {
                        mAdaptiveValue.variable5 = mManualStatVarProps.StatVar5.var5;
                    }

                    mAdaptiveValue.fk_variable = mManualStatVarProps.fk_variable;

                    // Get/set the measurement unit
                    mAdaptiveValue.enhet = mManualStatVarProps.MeasurementUnit;
                    if (mAdaptiveValue.enhet == null)
                    {
                        mMessages.AddMessage(MessagesErrors.MUnitNotSet);
                    }

                    // Add date, if available
                    if (pFrm.StatDatumProperties != null)
                    {
                        // Add logic to parse the date value into its individual parts here
                        var mStatDatum = new StatDateParser((string)pFrm.StatDatumProperties.GetValue(mR, mC), (string)pFrm.cbStatDatumFormat.SelectedValue);

                        if (mStatDatum.Success == true)
                        {
                            mAdaptiveValue.ar = mStatDatum.Year;
                            mAdaptiveValue.mnd = mStatDatum.Month;
                            //mAdaptiveValue.Day = mStatDatum.Day;
                            mAdaptiveValue.kvartal = mStatDatum.Quarter;
                        }
                        else
                        {
                            mMessages.AddMessage(MessagesErrors.AutoDateParseError);
                        }
                    }
                    else
                    {
                        // Get the values from the datagridview with statistical variables
                        if (mManualStatVarProps.Year != null)
                        {
                            mAdaptiveValue.ar = mManualStatVarProps.Year;
                            mMessages.AddMessage(MessagesErrors.ManualDateNotSet);
                        }

                        if (mManualStatVarProps.Quarter != null)
                        {
                            mAdaptiveValue.kvartal = mManualStatVarProps.Quarter;
                            mMessages.AddMessage(MessagesNotices.ManualQuarterNotSet);
                        }

                        if (mManualStatVarProps.Month != null)
                        {
                            mAdaptiveValue.mnd = mManualStatVarProps.Month;
                            mMessages.AddMessage(MessagesNotices.ManualMonthNotSet);
                        }

                        if (mManualStatVarProps.Day != null)
                        {
                            mAdaptiveValue.Day = mManualStatVarProps.Day;
                            mMessages.AddMessage(MessagesNotices.ManualDayNotSet);
                        }

                    }
                    if (pFrm.StatAreaIDsProperties != null || pFrm.StatAreaNameProperties != null || pFrm.StatAreaGroupProperties != null)
                    {
                        // Add statareaID if available
                        if (pFrm.StatAreaIDsProperties != null)
                        {
                            mAdaptiveValue.krets_id = (string)pFrm.StatAreaIDsProperties.GetValue(mR, mC);
                        }

                        // Statareaname is only relevant if id is present
                        if (pFrm.StatAreaNameProperties != null)
                        {
                            mAdaptiveValue.krets_navn = (string)pFrm.StatAreaNameProperties.GetValue(mR, mC);
                        }

                        // Statarea group is only relevant if id is present
                        if (pFrm.StatAreaGroupProperties != null)
                        {
                            mAdaptiveValue.region = (string)pFrm.StatAreaGroupProperties.GetValue(mR, mC);
                        }

                    }
                    // If no auto-properties, pick values from manual form fields
                    else
                    {
                        mAdaptiveValue.krets_id = pFrm.tbStatUnitID.Text;
                        mAdaptiveValue.krets_navn = pFrm.tbStatUnitName.Text;
                        mAdaptiveValue.region = pFrm.tbStatUnitGroup.Text;
                    }

                    mData.AddByKey(mAdaptiveValue, mR, mC);
                }
            }
            mTimer.Stop();
            pFrm.Log(mMessages.GetMessages());
            pFrm.Log(String.Format("Ferdig på {0} ms", mTimer.ElapsedMilliseconds));

            return mData;
        }