/// <summary>
        /// 方法说明:增加明细框Grid
        /// 作者:jason.tang
        /// 完成时间:2013-02-05
        /// </summary>
        /// <param name="top">上边距</param>
        /// <param name="left">下边距</param>
        /// <param name="objColumns">明细列集合</param>
        /// <param name="cellEx">单元格</param>
        private void AddDetailGridView(int top, int left, object objColumns, DataGridViewTextBoxCellEx cellEx, DataGridView datagridview, List <DetailGridViewTextBoxColumn> _dicColumns)
        {
            DataGridView dgv = new DataGridView();

            dgv.Name = string.Format("dgv{0}&{1}&{2}@{3}", cellEx.RowIndex, cellEx.ColumnIndex,
                                     Guid.NewGuid().ToString(), pageCount);
            dgv.BackgroundColor = Color.White;

            #region 明细框位置设定

            dgv.Top  = top;
            dgv.Left = left;

            //dgv.Name = string.Format("dgv{0}-{1}", cellEx.RowIndex.ToString(), cellEx.ColumnIndex.ToString());

            List <DetailGridViewTextBoxColumn> listColumns = new List <DetailGridViewTextBoxColumn>();
            if (objColumns != null)
            {
                DetailGridViewTextBoxColumn textBoxColumn;
                DetailCell[] detailCells = new DetailCell[] { };
                string       type        = objColumns.GetType().ToString();
                if (objColumns.GetType() == detailCells.GetType())
                {
                    detailCells = objColumns as DetailCell[];
                    foreach (DetailCell cell in detailCells)
                    {
                        textBoxColumn                    = new DetailGridViewTextBoxColumn();
                        textBoxColumn.Width              = cell.ColumnWidth;
                        textBoxColumn.Content            = cell.Content;
                        textBoxColumn.DetailSplitLine    = cell.DetailLine;
                        textBoxColumn.HeaderText         = cell.HeaderText;
                        textBoxColumn.Lans               = cell.Lans;
                        textBoxColumn.Length             = cell.Length;
                        textBoxColumn.ColumnName         = cell.Name;
                        textBoxColumn.PerProcessRow      = cell.PerProcessRow;
                        textBoxColumn.SplitLineInProcess = cell.ProcessDetailLine;
                        textBoxColumn.Rows               = cell.Rows.ToString();
                        textBoxColumn.SerialNumber       = cell.SerialNumber;
                        textBoxColumn.Source             = cell.Source;
                        textBoxColumn.SpaceRows          = cell.SpaceRows.ToString();
                        textBoxColumn.Tag                = cell.Tag;
                        textBoxColumn.Type               = string.IsNullOrEmpty(cell.Type) ? (ComboBoxSourceHelper.CellStyle)Enum.Parse(typeof(ComboBoxSourceHelper.CellStyle), "0") :
                                                           (ComboBoxSourceHelper.CellStyle)Enum.Parse(typeof(ComboBoxSourceHelper.CellStyle), cell.Type);
                        textBoxColumn.Visible         = cell.AdvanceProperty != ((ComboBoxSourceHelper.AdvanceProperty)Enum.Parse(typeof(ComboBoxSourceHelper.AdvanceProperty), "3")).ToString();
                        textBoxColumn.AdvanceProperty = !string.IsNullOrEmpty(cell.AdvanceProperty) ? (ComboBoxSourceHelper.AdvanceProperty)Enum.Parse(typeof(ComboBoxSourceHelper.AdvanceProperty), cell.AdvanceProperty) :
                                                        (ComboBoxSourceHelper.AdvanceProperty)Enum.Parse(typeof(ComboBoxSourceHelper.AdvanceProperty), "0");
                        textBoxColumn.SerialStep  = cell.SerialStep;
                        textBoxColumn.ColumnValue = cell.ColumnValue;

                        listColumns.Add(textBoxColumn);

                        if (_dicColumns != null && !_dicColumns.Contains(textBoxColumn))
                        {
                            _dicColumns.Add(textBoxColumn);
                        }
                    }
                }
            }

            dgv.Width  = datagridview.Columns[cellEx.ColumnIndex].Width;
            dgv.Height = datagridview.Rows[cellEx.RowIndex].Height;

            #endregion

            #region 明细框:处理合并的单元格

            int colspan = cellEx.ColumnSpan;
            int rowspan = cellEx.RowSpan;

            if (colspan > 1)
            {
                for (int i = 1; i < colspan; i++)
                {
                    dgv.Width += datagridview.Columns[cellEx.ColumnIndex + i].Width;
                }
            }
            if (rowspan > 1)
            {
                for (int i = 1; i < rowspan; i++)
                {
                    dgv.Height += datagridview.Rows[cellEx.RowIndex + i].Height;
                }
            }

            #endregion

            #region 明细框列表属性配置

            dgv.RowHeadersVisible = false;
            dgv.BorderStyle       = BorderStyle.None;
            //dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgv.ReadOnly                    = true;
            dgv.ScrollBars                  = ScrollBars.None;
            dgv.BorderStyle                 = BorderStyle.None;
            dgv.AllowUserToAddRows          = false;
            dgv.AllowUserToResizeColumns    = false;
            dgv.AllowUserToResizeRows       = false;
            dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            //dgv.EditMode = DataGridViewEditMode.EditOnEnter;
            if (listColumns.Count > 0)
            {
                dgv.Rows.Clear();
                dgv.Columns.Clear();
                dgv.DefaultCellStyle.SelectionBackColor = Color.WhiteSmoke;
                dgv.DefaultCellStyle.SelectionForeColor = Color.Black;
                int    totalWidth = 0;
                int    totalRows  = 1;
                string tag        = string.Empty;
                foreach (DetailGridViewTextBoxColumn column in listColumns)
                {
                    dgv.Columns.Add(column);
                    totalWidth += column.Width;
                }

                if (totalWidth < dgv.Width)
                {
                    dgv.Columns[dgv.Columns.Count - 1].Width += dgv.Width - totalWidth;
                }

                if (listColumns != null && listColumns.Count > 0)
                {
                    DetailGridViewTextBoxColumn columns = (DetailGridViewTextBoxColumn)listColumns[0];
                    totalRows = int.Parse(columns.Rows);
                    for (int i = 0; i < totalRows; i++)
                    {
                        dgv.Rows.Insert(i, 1);
                    }
                }

                int rowHeight = dgv.Height - 23;
                //总宽度不足,补充到最后一列
                for (int i = 0; i < totalRows; i++)
                {
                    dgv.Rows[i].Height = rowHeight / totalRows;
                    if (i == totalRows - 1)
                    {
                        dgv.Rows[i].Height = rowHeight / totalRows + rowHeight % totalRows;
                    }
                }
            }

            int colRange = 0;
            foreach (DetailGridViewTextBoxColumn column in listColumns)
            {
                string columnValue = ((DetailGridViewTextBoxColumn)column).ColumnValue;
                if (!string.IsNullOrEmpty(columnValue) && string.IsNullOrEmpty(column.Source))
                {
                    string[] strValues = columnValue.Split(new char[] { ',' });
                    int      rowRange  = 0;
                    foreach (DataGridViewRow row in dgv.Rows)
                    {
                        row.Cells[colRange].Value = strValues[rowRange];
                        rowRange++;
                    }
                }//来源不为空,则自动带出来源数据
                else if (!string.IsNullOrEmpty(column.Source) && ModuleObject != null)
                {
                    foreach (PropertyInfo pi in ModuleObject.GetType().GetProperties())
                    {
                        if (pi.Name.ToLower() == column.Source.ToLower() && pi.GetValue(ModuleObject, new object[] { }) != null)
                        {
                            columnValue = pi.GetValue(ModuleObject, new object[] { }).ToString();
                            break;
                        }
                    }
                    int rowRange = 0;
                    foreach (DataGridViewRow row in dgv.Rows)
                    {
                        row.Cells[colRange].Value = columnValue;
                        rowRange++;
                    }
                }

                colRange++;
            }

            //dgv.CellClick += dgv_CellClick;
            //dgv.CellDoubleClick += DataGridView_CellDoubleClick;
            //dgv.CellMouseDown += DataGridView_CellMouseDown;
            //dgv.EditingControlShowing += dgv_EditingControlShowing;
            //dgv.KeyDown += dgv_KeyDown;
            //dgv.CellBeginEdit += dgv_CellBeginEdit;

            //dgvTemp = dgv;
            SetSerialNumberColumn(dgv);
            datagridview.Controls.Add(dgv);

            #endregion
        }
Esempio n. 2
0
        /// <summary>
        /// 方法说明:增加明细框Grid
        /// 作者:jason.tang
        /// 完成时间:2012-12-22
        /// </summary>
        /// <param name="top">上边距</param>
        /// <param name="left">下边距</param>
        /// <param name="objColumns">明细列集合</param>
        private void AddDetailGridView(int top, int left, object objColumns, List <DetailGridViewTextBoxColumn> _dicColumns, DataGridView datagridview, DataGridViewTextBoxCellEx cellEx)
        {
            DataGridView dgv = new DataGridView();

            dgv.BackgroundColor = Color.White;

            #region 明细框位置设定

            dgv.Top  = top;
            dgv.Left = left;

            CheckIsInSpanCells(dgv, datagridview, cellEx);

            dgv.Name = string.Format("dgv{0}-{1}", cellEx.RowIndex.ToString(), cellEx.ColumnIndex.ToString());

            List <DetailGridViewTextBoxColumn> listColumns = new List <DetailGridViewTextBoxColumn>();
            if (objColumns != null)
            {
                List <DetailGridViewTextBoxColumn> dicColumns = objColumns as List <DetailGridViewTextBoxColumn>;
                DetailGridViewTextBoxColumn        textBoxColumn;
                DetailCell[] detailCells = new DetailCell[] { };
                string       type        = objColumns.GetType().ToString();
                if (objColumns.GetType() == detailCells.GetType())
                {
                    detailCells = objColumns as DetailCell[];
                    foreach (DetailCell cell in detailCells)
                    {
                        textBoxColumn                    = new DetailGridViewTextBoxColumn();
                        textBoxColumn.Width              = cell.ColumnWidth;
                        textBoxColumn.Content            = cell.Content;
                        textBoxColumn.DetailSplitLine    = cell.DetailLine;
                        textBoxColumn.HeaderText         = cell.HeaderText;
                        textBoxColumn.Lans               = cell.Lans;
                        textBoxColumn.Length             = cell.Length;
                        textBoxColumn.ColumnName         = cell.Name;
                        textBoxColumn.PerProcessRow      = cell.PerProcessRow;
                        textBoxColumn.SplitLineInProcess = cell.ProcessDetailLine;
                        textBoxColumn.Rows               = cell.Rows.ToString();
                        textBoxColumn.SerialNumber       = cell.SerialNumber;
                        textBoxColumn.Source             = cell.Source;
                        textBoxColumn.SpaceRows          = cellEx.SpaceRows.ToString(); //cell.SpaceRows.ToString();
                        textBoxColumn.Tag                = cell.Tag;
                        textBoxColumn.Type               = string.IsNullOrEmpty(cell.Type) ? (ComboBoxSourceHelper.CellStyle)Enum.Parse(typeof(ComboBoxSourceHelper.CellStyle), "0") :
                                                           (ComboBoxSourceHelper.CellStyle)Enum.Parse(typeof(ComboBoxSourceHelper.CellStyle), cell.Type);
                        textBoxColumn.Visible         = cell.AdvanceProperty != ((ComboBoxSourceHelper.AdvanceProperty)Enum.Parse(typeof(ComboBoxSourceHelper.AdvanceProperty), "3")).ToString();
                        textBoxColumn.AdvanceProperty = !string.IsNullOrEmpty(cell.AdvanceProperty) ? (ComboBoxSourceHelper.AdvanceProperty)Enum.Parse(typeof(ComboBoxSourceHelper.AdvanceProperty), cell.AdvanceProperty) :
                                                        (ComboBoxSourceHelper.AdvanceProperty)Enum.Parse(typeof(ComboBoxSourceHelper.AdvanceProperty), "0");
                        textBoxColumn.SerialStep = cellEx.SerialStep; //cell.SerialStep;
                        listColumns.Add(textBoxColumn);
                        if (_dicColumns != null && !_dicColumns.Contains(textBoxColumn))
                        {
                            _dicColumns.Add(textBoxColumn);
                        }
                    }
                }
                else if (dicColumns != null)
                {
                    foreach (DetailGridViewTextBoxColumn col in dicColumns)
                    {
                        col.SerialStep = cellEx.SerialStep;
                        col.SpaceRows  = cellEx.SpaceRows.ToString();
                        listColumns.Add(col);
                    }
                }
            }

            if (datagridview.Controls.ContainsKey(dgv.Name) && objColumns != null)
            {
                SetDataGridViewColumns(listColumns, dgv.Name, datagridview);
                return;
            }

            dgv.Width  = datagridview.Columns[datagridview.CurrentCell.ColumnIndex].Width;
            dgv.Height = datagridview.Rows[datagridview.CurrentCell.RowIndex].Height;

            #endregion

            #region 明细框:处理合并的单元格

            int colspan = (datagridview.CurrentCell as DataGridViewTextBoxCellEx).ColumnSpan;
            int rowspan = (datagridview.CurrentCell as DataGridViewTextBoxCellEx).RowSpan;

            if (colspan > 1)
            {
                for (int i = 1; i < colspan; i++)
                {
                    dgv.Width += datagridview.Columns[datagridview.CurrentCell.ColumnIndex + i].Width;
                }
            }
            if (rowspan > 1)
            {
                for (int i = 1; i < rowspan; i++)
                {
                    dgv.Height += datagridview.Rows[datagridview.CurrentCell.RowIndex + i].Height;
                }
            }

            #endregion

            #region 明细框列表属性配置

            Control[] controls = datagridview.Controls.Find(dgv.Name, false);
            if (controls.Length > 0)
            {
                dgv = (DataGridView)controls[0];
                dgv.Rows.Clear();
                dgv.Columns.Clear();
            }


            dgv.Columns.Add("", "");
            dgv.Columns.Add("", "");
            //dgv.ColumnHeadersVisible = false;
            dgv.Rows[0].Height   = dgv.Height;
            dgv.Columns[0].Width = 25;
            dgv.Columns[1].Width = dgv.Width - 25;
            dgv.DefaultCellStyle.SelectionBackColor = Color.WhiteSmoke;
            dgv.BorderStyle = BorderStyle.None;
            //dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgv.RowHeadersVisible           = false;
            dgv.ReadOnly                    = true;
            dgv.ScrollBars                  = ScrollBars.None;
            dgv.AutoGenerateColumns         = false;
            dgv.AllowUserToResizeColumns    = true;
            dgv.AllowUserToResizeRows       = false;
            dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            //dgv.RowHeightChanged += dgv_RowHeightChanged;
            datagridview.Controls.Add(dgv);
            if (listColumns.Count > 0)
            {
                SetDataGridViewColumns(listColumns, dgv.Name, datagridview);
            }

            #endregion
        }