/// <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 }
private void GetCard(ProcessCard _card, DataGridView datagridview) { ProcessCardBLL pcBll = new ProcessCardBLL(); ProcessCard processCard = new ProcessCard(); CardsXML cards = new CardsXML(); //DataGridView datagridview = new DataGridView(); Guid cardid = _card.ID; if (cardid != null) { processCard = pcBll.GetProcessCard(cardid); cards = processCard.Card; //if (!isNew) // this.Tag = id; } if (cards == null) { return; } datagridview.Tag = processCard.Name; int index = 0; //Panel pn = new Panel(); foreach (Card card in cards.Cards) { //pn = new Panel(); //pn.BorderStyle = BorderStyle.FixedSingle; //pn.Width = pnlPlanningCard.Width; //pn.Height = pnlPlanningCard.Height; //pn.AutoScroll = false; //pn.Name = string.Format("pnCard{0}@{1}", Guid.NewGuid().ToString(), pageCount + 1); if (index > 0) { datagridview = new DataGridView(); datagridview.Name = string.Format("dgvCard{0}", card.Id.ToString()); datagridview.RowHeadersVisible = false; datagridview.ColumnHeadersVisible = false; //datagridview.Dock = DockStyle.Top; datagridview.AllowUserToResizeColumns = false; datagridview.AllowUserToResizeRows = false; datagridview.ReadOnly = true; datagridview.ScrollBars = ScrollBars.None; datagridview.DefaultCellStyle.SelectionBackColor = datagridview.DefaultCellStyle.BackColor; datagridview.DefaultCellStyle.SelectionForeColor = Color.Black; datagridview.AutoGenerateColumns = false; pnlPlanningCard.Controls.Add(datagridview); } index++; int Width = Convert.ToInt32(card.Width); int Height = Convert.ToInt32(card.Height); int breadth = int.Parse(card.CardRange.Replace("A", "")); cardWidth = Width; cardHeight = Height; cardBreadth = breadth; //if (datagridview.Name == "dgvCard") //{ ResizeControls(Width, Height, breadth, datagridview); //} List <DataGridViewRow> listRow = new List <DataGridViewRow>(); if (card.Rows == null) { continue; } int rows = card.Rows.Length; int columns = card.Rows[0].Cells.Length; List <int> listWidth = new List <int>(); List <int> listHeight = new List <int>(); foreach (Row row in card.Rows) { listHeight.Add(Convert.ToInt32(row.Height)); } foreach (Cell cell in card.Rows[0].Cells) { listWidth.Add(Convert.ToInt32(cell.Width)); } InitDataGridView(listHeight, listWidth, datagridview); DataGridViewTextBoxCellEx cellEx; List <DataGridViewCustomerCellStyle> listCellStyle; DataGridViewCustomerCellStyle cellStyle; List <int> listPadding; try { foreach (Row row in card.Rows) { foreach (Cell cell in row.Cells) { cellEx = ((DataGridViewTextBoxCellEx)datagridview.Rows[cell.PointX].Cells[cell.PointY]); cellEx.CellTag = processCard.Name; cellEx.Style.Alignment = cell.Alignment == null ? DataGridViewContentAlignment.NotSet : (DataGridViewContentAlignment)Enum.Parse(typeof(DataGridViewContentAlignment), cell.Alignment); cellEx.Style.BackColor = cell.BackGround == null || int.Parse(cell.BackGround) == 0 ? Color.Empty : Color.FromArgb(int.Parse(cell.BackGround)); cellEx.BottomBorderColor = cell.BottomBorderColor == null || int.Parse(cell.BottomBorderColor) == 0 ? Color.Empty : Color.FromArgb(int.Parse(cell.BottomBorderColor)); cellEx.BottomBorderWidth = Convert.ToInt32(cell.BottomBorderWidth); cellEx.CellEditType = (ComboBoxSourceHelper.CellType)Enum.Parse(typeof(ComboBoxSourceHelper.CellType), cell.CellType); cellEx.ColumnSpan = cell.ColSpan; if (!string.IsNullOrEmpty(cellEx.CellSource) && ModuleObject != null) { foreach (PropertyInfo pi in ModuleObject.GetType().GetProperties()) { if (pi.Name.ToLower() == cellEx.CellSource.ToLower()) { cellEx.Value = pi.GetValue(ModuleObject, new object[] { }); break; } } } else { cellEx.Value = cell.Content; } string[] spanCell = cell.SpanCell.Split(new char[] { ',' }); cellEx.SpanCell = new Point(int.Parse(spanCell[0]), int.Parse(spanCell[1])); #region 处理CellStyle listCellStyle = new List <DataGridViewCustomerCellStyle>(); listPadding = new List <int>(); if (cellEx.CustStyle == null) { cellStyle = new DataGridViewCustomerCellStyle(); cellStyle.Alignment = cell.Alignment == null ? DataGridViewContentAlignment.NotSet : (DataGridViewContentAlignment)Enum.Parse(typeof(DataGridViewContentAlignment), cell.Alignment); cellStyle.BackColor = cell.BackGround == null || int.Parse(cell.BackGround) == 0 ? Color.Empty : Color.FromArgb(int.Parse(cell.BackGround)); if (cell.FontFamily == null || cell.ZoomFontSize == null || cell.FontStyle == null) { cellStyle.Font = preFont; } else { cellStyle.Font = new Font(cell.FontFamily, float.Parse(cell.ZoomFontSize), (FontStyle)Enum.Parse(typeof(FontStyle), cell.FontStyle)); } cellStyle.ForeColor = cell.ForeColor == null ? preColor : Color.FromArgb(int.Parse(cell.ForeColor)); preFont = cellStyle.Font; preColor = cellStyle.ForeColor; cellStyle.WrapMode = (DataGridViewTriState)Enum.Parse(typeof(DataGridViewTriState), cell.WrapMode.ToString()); if (cell.Padding == null) { listPadding.Add(0); listPadding.Add(0); listPadding.Add(0); listPadding.Add(0); } else { string[] padding = cell.Padding.Split(new char[] { ',' }); foreach (string pad in padding) { listPadding.Add(int.Parse(pad)); } } cellStyle.Padding = new System.Windows.Forms.Padding(listPadding[0], listPadding[1], listPadding[2], listPadding[3]); listCellStyle.Add(cellStyle); cellEx.CustStyle = listCellStyle; } #endregion //cell.ContentType; //cell.DataSrc; //cell.DetailCells; if (cell.FontFamily == null || cell.FontSize == null || cell.FontStyle == null) { cellEx.Style.Font = preFont; } else { cellEx.Style.Font = new Font(cell.FontFamily, float.Parse(cell.FontSize), (FontStyle)Enum.Parse(typeof(FontStyle), cell.FontStyle)); } cellEx.Style.ForeColor = cell.ForeColor == null ? preColor : Color.FromArgb(int.Parse(cell.ForeColor)); preFont = cellEx.Style.Font; preColor = cellEx.Style.ForeColor; cellEx.LeftBorderColor = Color.FromArgb(int.Parse(cell.LeftBorderColor)); cellEx.LeftBorderWidth = Convert.ToInt32(cell.LeftBorderWidth); //cell.Name; cellEx.RightBorderColor = Color.FromArgb(int.Parse(cell.RightBorderColor)); cellEx.RightBorderWidth = Convert.ToInt32(cell.RightBorderWidth); cellEx.RowSpan = cell.RowSpan; cellEx.TopBorderColor = Color.FromArgb(int.Parse(cell.TopBorderColor)); cellEx.TopBorderWidth = Convert.ToInt32(cell.TopBorderWidth); cellEx.LeftTopRightBottom = cell.LeftTopRightBottom; cellEx.LeftBottomRightTop = cell.LeftBottomRightTop; cellEx.CellContent = string.IsNullOrEmpty(cell.ContentType) ? (ComboBoxSourceHelper.CellContent)Enum.Parse(typeof(ComboBoxSourceHelper.CellContent), "0") : (ComboBoxSourceHelper.CellContent)Enum.Parse(typeof(ComboBoxSourceHelper.CellContent), cell.ContentType); cellEx.Style.WrapMode = (DataGridViewTriState)Enum.Parse(typeof(DataGridViewTriState), cell.WrapMode.ToString()); cellEx.CellSource = cell.CellSource; string parentName = datagridview.Parent.Name; //明细框单元格 if (cellEx.CellEditType == (ComboBoxSourceHelper.CellType)Enum.Parse(typeof(ComboBoxSourceHelper.CellType), "2")) { Rectangle rect = datagridview.GetCellDisplayRectangle(cellEx.ColumnIndex, cellEx.RowIndex, false); int top = rect.Y - 1; int left = rect.X - 1; List <DetailGridViewTextBoxColumn> dicColumns = new List <DetailGridViewTextBoxColumn>(); object objDetailProperty = cell.DetailCells; //明细框 AddDetailGridView(top, left, objDetailProperty, cellEx, datagridview, dicColumns); cellEx.DetailProperty = dicColumns; } else if (cellEx.CellEditType == (ComboBoxSourceHelper.CellType)Enum.Parse(typeof(ComboBoxSourceHelper.CellType), "3")) //页码 { cellEx.Value = 1; } else if (cellEx.CellEditType == (ComboBoxSourceHelper.CellType)Enum.Parse(typeof(ComboBoxSourceHelper.CellType), "4")) //页数 { cellEx.Value = 1; } } } //读取图片 if (card.ImageObjects != null && card.ImageObjects.Length > 0) { foreach (ImageObject image in card.ImageObjects) { LoadImage(image, datagridview); } } } catch { MessageBox.Show("读取模板文件失败,无法新建卡片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } }