private void cmdOK_Click(object sender, EventArgs e)
        {
            if (!ValidateForm())
            {
                this.DialogResult = DialogResult.None;
                return;
            }

            RasterOperators.MultiMathOpType eMethod = (RasterOperators.MultiMathOpType)((naru.db.NamedObject)cboMethod.SelectedItem).ID;

            List <Tuple <DEMSurvey, ErrorSurface> > rInputs = new List <Tuple <DEMSurvey, ErrorSurface> >();

            for (int i = 0; i < grdData.Rows.Count; i++)
            {
                DEMItem dem = grdData.Rows[i].DataBoundItem as DEMItem;
                if (!dem.Include)
                {
                    continue;
                }

                DataGridViewComboBoxCell comboCell = grdData.Rows[i].Cells["colError"] as DataGridViewComboBoxCell;
                BindingSource            bs        = comboCell.DataSource as BindingSource;
                ErrorSurface             err       = bs.Current as ErrorSurface;

                rInputs.Add(new Tuple <DEMSurvey, ErrorSurface>(dem._DEM, err));
            }

            Engines.ReferenceSurfaceEngine eng = new Engines.ReferenceSurfaceEngine(txtName.Text, rInputs, eMethod);

            System.IO.FileInfo fiOutput = ProjectManager.Project.GetAbsolutePath(txtPath.Text);
            System.IO.FileInfo fiError  = Surface.ErrorSurfaceRasterPath(fiOutput.Directory, txtName.Text);

            try
            {
                Cursor = Cursors.WaitCursor;

                Surface surf = eng.Run(fiOutput, fiError);
                ProjectManager.AddNewProjectItemToMap(surf);
                Cursor = Cursors.Default;
                MessageBox.Show("Reference surface generated successfully.", Properties.Resources.ApplicationNameLong, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                GCDException.HandleException(ex, "Error generating reference surface from DEM surveys.");
                this.DialogResult = DialogResult.None;
            }
        }
        private bool ValidateForm()
        {
            // Sanity check to avoid empty names
            txtName.Text.Trim();

            if (string.IsNullOrEmpty(txtName.Text))
            {
                MessageBox.Show("You must provide a name for the reference surface.", "Missing Name", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtName.Select();
                return(false);
            }

            // Validate that every DEM has an error surface selected
            foreach (DataGridViewRow dgvr in grdData.Rows)
            {
                DEMItem dem = dgvr.DataBoundItem as DEMItem;
                if (dem.Include)
                {
                    DataGridViewComboBoxCell comboCell = dgvr.Cells["colError"] as DataGridViewComboBoxCell;
                    if (comboCell.Value == null)
                    {
                        grdData.Select();
                        comboCell.Selected = true;
                        MessageBox.Show("You must select an error surface for all DEM Surveys that are to be included in the operation.", "Missing Error Surface", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return(false);
                    }
                }
            }

            if (!GCDCore.Project.ProjectManager.Project.IsReferenceSurfaceNameUnique(txtName.Text, null))
            {
                MessageBox.Show("The GCD project already contains a reference surface with this name. Please choose a unique name for the reference surface.", "Name Already Exists", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtName.Select();
                return(false);
            }

            if (DEMSurveys.Count(x => x.Include) < 2)
            {
                MessageBox.Show("You must select at least two DEM surveys to generate a reference surface.", "Insufficient DEM Surveys", MessageBoxButtons.OK, MessageBoxIcon.Information);
                grdData.Select();
                return(false);
            }

            return(true);
        }