コード例 #1
0
 public override void  Remove(Control value)
 {
     if (DataGridViewSubForm.IsDataGridViewSubForm(value.GetType()))
     {
         if (((DataGridViewSubForm)value).IsShowing)
         {
             return;
         }
     }
     base.Remove(value);
 }
コード例 #2
0
 public SubformClosingEventArgs(DataGridViewSubForm Form)
 {
     Cancel = false;
     _form  = Form;
 }
コード例 #3
0
        /// <summary>
        /// Shows the from with correct alignment
        /// </summary>
        public void ShowForm()
        {
            if (this.OwningColumn == null)
            {
                return;
            }
            if (((DataGridViewSubformColumn)this.OwningColumn).Subform == null)
            {
                return;
            }
            //save the previous alignment of cells for when the subform is closed and set new alignments to be the top
            alignmentBeforeShow = new object[OwningRow.Cells.Count, 2];
            for (int i = 0; i < OwningRow.Cells.Count; i++)
            {
                alignmentBeforeShow[i, 0] = OwningRow.Cells[i].Style.Alignment;
                alignmentBeforeShow[i, 1] = OwningRow.Cells[i].OwningColumn;
                if (OwningRow.Cells[i].Style.Alignment == DataGridViewContentAlignment.BottomCenter ||
                    OwningRow.Cells[i].Style.Alignment == DataGridViewContentAlignment.MiddleCenter)
                {
                    OwningRow.Cells[i].Style.Alignment = DataGridViewContentAlignment.TopCenter;
                }
                else if (OwningRow.Cells[i].Style.Alignment == DataGridViewContentAlignment.BottomLeft ||
                         OwningRow.Cells[i].Style.Alignment == DataGridViewContentAlignment.MiddleLeft)
                {
                    OwningRow.Cells[i].Style.Alignment = DataGridViewContentAlignment.TopLeft;
                }
                else if (OwningRow.Cells[i].Style.Alignment == DataGridViewContentAlignment.BottomRight ||
                         OwningRow.Cells[i].Style.Alignment == DataGridViewContentAlignment.MiddleRight)
                {
                    OwningRow.Cells[i].Style.Alignment = DataGridViewContentAlignment.TopRight;
                }
                else if (OwningRow.Cells[i].Style.Alignment == DataGridViewContentAlignment.NotSet)
                {
                    if (OwningRow.Cells[i].GetType() == typeof(DataGridViewCheckBoxCell))
                    {
                        OwningRow.Cells[i].Style.Alignment = DataGridViewContentAlignment.TopCenter;
                    }
                    else
                    {
                        OwningRow.Cells[i].Style.Alignment = DataGridViewContentAlignment.TopLeft;
                    }
                }
            }
            heightBeforeShowSubForm = RowSubformCells.Max(r => r.heightBeforeShowSubForm);
            if (heightBeforeShowSubForm == 0)
            {
                heightBeforeShowSubForm = OwningRow.Height; //save the original heigh of the row
            }
            //Fire the subformshowing event to get a possible alternate subform or cancel the showing
            SubformShowingEventArgs ea = new SubformShowingEventArgs();

            ((DataGridViewSubformColumn)this.OwningColumn).RequestSubformShowing(this, ea);
            Type t = null;

            if (ea.Cancel)
            {
                _subformshowing = false;
                return;
            }
            if (ea.AlternateForm != null)
            {
                t = ea.AlternateForm;
            }
            else
            {
                t = ((DataGridViewSubformColumn)this.OwningColumn).Subform;
            }

            //set the data for the subform
            object data = null;

            if (ea.DataBoundItem != null)
            {
                data = ea.DataBoundItem;
            }
            else
            {
                if (((DataGridViewSubformColumn)this.OwningColumn).SubformDataMember == "this")
                {
                    data = OwningRow.DataBoundItem;
                }
                else
                {
                    try
                    {
                        //follow members of member e.g. Shift.Employee.DateOfBirth
                        string[] parts = ((DataGridViewSubformColumn)this.OwningColumn).SubformDataMember.Split('.');
                        data = OwningRow.DataBoundItem;
                        foreach (string member in parts)
                        {
                            System.Reflection.PropertyInfo pi = data.GetType().GetProperty(member);
                            data = pi.GetValue(OwningRow.DataBoundItem, null);
                        }
                    }
                    catch (Exception e)
                    {
                        if (!((DataGridViewSubformColumn)this.OwningColumn).IgnoreSubformDataBindingError)
                        {
                            throw (e);
                        }
                    }
                }
            }

            //create the subform
            form = (DataGridViewSubForm)Activator.CreateInstance(t, data, this);
            PositionSubform();
            //set the new row height to be the same as the subform plus the original height
            if (((DataGridViewExtension)DataGridView).SubFormColumns.Count() > 1)
            {
                this.OwningRow.Height = heightBeforeShowSubForm +
                                        RowSubformCells.Max(r => r.SubformHeight);
            }
            else
            {
                this.OwningRow.Height = this.OwningRow.Height + form.Height;
            }
            //add the subform to the Data grid view
            DataGridView.Controls.Add(form);
            //reposition all open subforms
            for (int i = OwningRow.Index + 1; i < DataGridView.Rows.Count; i++)
            {
                foreach (DataGridViewSubformColumn col in ((DataGridViewExtension)DataGridView).SubFormColumns)
                {
                    DataGridViewSubformCell cell = (DataGridViewSubformCell)DataGridView.Rows[i].Cells[col.Index];
                    if (cell.SubformShowing)
                    {
                        cell.PositionSubform();
                    }
                }
            }

            //set the z index for the new subform to be top most
            int childIndex = Int32.MaxValue;

            foreach (DataGridViewSubformColumn col in ((DataGridViewExtension)DataGridView).SubFormColumns)
            {
                int index = DataGridView.Controls.IndexOf(((DataGridViewSubformCell)OwningRow.Cells[col.Index]).form);
                if (index < childIndex)
                {
                    childIndex = index;
                }
            }
            if (childIndex < DataGridView.Controls.IndexOf(form))
            {
                DataGridView.Controls.SetChildIndex(form, childIndex);
            }
        }
コード例 #4
0
 public void CloseForm()
 {
     if (this.OwningColumn == null)
     {
         return;
     }
     if (form != null)
     {
         //Fire the subformclosing event and check for cancels
         SubformClosingEventArgs ea = new SubformClosingEventArgs(form);
         ((DataGridViewSubformColumn)this.OwningColumn).RequestSubformClosing(this, ea);
         if (ea.Cancel)
         {
             _subformshowing = true;
             return;
         }
         //remove the subform from the data gridview
         DataGridView.Controls.Remove(form);
         //dispose of the subform
         form.Dispose();
         form = null;
         //set the height of the row to the correct value
         if (((DataGridViewExtension)DataGridView).SubFormColumns.Count() > 1)
         {
             if (RowSubformCells.Max(r => r.SubformHeight) > 0)
             {
                 OwningRow.Height = RowSubformCells.Max(r => r.SubformHeight) + heightBeforeShowSubForm;
             }
             else
             {
                 OwningRow.Height        = heightBeforeShowSubForm;
                 heightBeforeShowSubForm = 0;
             }
         }
         else
         {
             OwningRow.Height        = heightBeforeShowSubForm;
             heightBeforeShowSubForm = 0;
         }
         //reset alignment for each cell
         if (OwningRow.Cells.Cast <DataGridViewCell>().Where(r => r.GetType() == typeof(DataGridViewSubformCell)).Cast <DataGridViewSubformCell>().Where(r => r.SubformShowing).Count() == 0)
         {
             for (int i = 0; i < alignmentBeforeShow.Length / 2; i++)
             {
                 if (OwningRow.Cells.Cast <DataGridViewCell>().Where(r => r.OwningColumn ==
                                                                     (DataGridViewColumn)alignmentBeforeShow[i, 1]).Count() > 0)
                 {
                     OwningRow.Cells.Cast <DataGridViewCell>().Where(r => r.OwningColumn ==
                                                                     (DataGridViewColumn)alignmentBeforeShow[i, 1]).First().Style.Alignment
                         = (DataGridViewContentAlignment)alignmentBeforeShow[i, 0];
                 }
             }
         }
         //reposition all subforms below this one
         for (int i = DataGridView.CurrentCell.RowIndex + 1; i < DataGridView.Rows.Count; i++)
         {
             foreach (DataGridViewSubformColumn col in ((DataGridViewExtension)DataGridView).SubFormColumns)
             {
                 DataGridViewSubformCell cell = ((DataGridViewSubformCell)DataGridView.Rows[i].Cells[col.Index]);
                 if (cell.SubformShowing)
                 {
                     cell.PositionSubform();
                 }
             }
         }
     }
 }