internal void RequestSubformShowing(DataGridViewSubformCell sender, SubformShowingEventArgs e)
 {
     if (SubformShowing != null)
     {
         SubformShowing(sender, e);
     }
 }
Пример #2
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);
            }
        }