void SetDefaultDate(DateTime d) { dataGridView1.DefaultValuesNeeded -= dataGridView1_DefaultValuesNeeded; dataGridView1.CellValidating -= dataGridView1_CellValidating; dataGridView1.CellValueChanged -= dataGridView1_CellValueChanged; dataGridView1.Rows.Clear(); if (_defaultDate.Year != d.Year) { dataGridView1.Columns[0].HeaderCell.Value = (d.Year + "年;月;日"); dataGridView1.Refresh(); } _defaultDate = d; dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically; //EditOnEnter; //EditOnKeystroke;// .EditProgrammatically; CustomDataGridView grid = dataGridView1; DateTime dt = DateTime.Now; int i = grid.Rows.Add(); grid[0, i].Value = dt.Month; grid[1, i].Value = dt.Day; grid[2, i].Value = "J"; grid[3, i].Value = "100"; grid[4, i].Value = "dg"; grid[5, i].Value = "dfgdhfgh"; grid[6, i].Value = "fhfgjghjghkjhgkgjkgh"; grid[7, i].Value = 134523.45.ToString("0.00"); grid[8, i].Value = 456456.ToString("0.00"); //余额列最后一起算.grid[9, i].Value = d; grid[10, i].Value = UnvaryingSagacity.Core.Printer.PrintAssign.OK_FLAG; grid.Rows[i].Tag = ""; // dataGridView1.RowValidated += new DataGridViewCellEventHandler(dataGridView1_RowValidated); dataGridView1.DefaultValuesNeeded += new DataGridViewRowEventHandler(dataGridView1_DefaultValuesNeeded); dataGridView1.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating); dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged); }
private void dataGridView1_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e) { const int CYTITLEHEIGHT = 25; CustomDataGridView grid = (sender as CustomDataGridView); #region Header: year,month,day,VCH;CHEQUE if ((e.ColumnIndex >= 0 && e.ColumnIndex <= 5) && e.RowIndex == -1 && _currentState != UIState.ViewTotal) { if (e.ColumnIndex == 0 || e.ColumnIndex == 2 || e.ColumnIndex == 4) { //e.Paint(e.ClipBounds, DataGridViewPaintParts.Background |DataGridViewPaintParts.Border ); Rectangle rt = e.CellBounds; Pen penGridLine = new Pen(grid.GridColor); Pen penWhite = new Pen(Color.White); rt.Width += grid.Columns[e.ColumnIndex + 1].Width; e.Graphics.FillRectangle(new SolidBrush(grid.RowHeadersDefaultCellStyle.BackColor), rt); e.Graphics.DrawLine(penGridLine, e.ClipBounds.X, e.ClipBounds.Y, e.ClipBounds.X, rt.Bottom); //Left e.Graphics.DrawLine(penGridLine, rt.X, rt.Y, rt.Right, rt.Y); //Top e.Graphics.DrawLine(penGridLine, rt.Right - 1, rt.Y, rt.Right - 1, rt.Bottom); //Right e.Graphics.DrawLine(penGridLine, rt.X, rt.Bottom - 1, rt.Right, rt.Bottom - 1); //bottom e.Graphics.DrawLine(penWhite, rt.Left + 1, rt.Top + 1, rt.Right, rt.Top + 1); //Top if (e.ColumnIndex == 0) { e.Graphics.DrawLine(penWhite, rt.X + 1, rt.Top + 1, rt.X + 1, rt.Bottom);//Left } else { e.Graphics.DrawLine(penWhite, rt.X, rt.Top + 1, rt.X, rt.Bottom);//Left } Rectangle rtYear = rt; Rectangle rtMonth = rt; Rectangle rtDay = rt; rtYear.Height = rt.Height / 2; rtMonth.Height = rtYear.Height; rtDay.Height = rtYear.Height; rtMonth.Width = rtYear.Width / 2; rtDay.Width = rtMonth.Width; rtMonth.Offset(0, rtYear.Height); rtDay.Offset(rtMonth.Width, rtYear.Height); e.Graphics.DrawLine(penGridLine, rtYear.Left + 2, rtYear.Bottom, rtYear.Right - 1, rtYear.Bottom);//center_H if (e.ColumnIndex == 0) { e.Graphics.DrawLine(penGridLine, rtDay.X, rtDay.Top, rtDay.X, rtDay.Bottom);//center_V } else { e.Graphics.DrawLine(penGridLine, rtDay.X - 1, rtDay.Top, rtDay.X - 1, rtDay.Bottom);//center_V } StringFormat _sf = new StringFormat(); _sf.Alignment = StringAlignment.Center; _sf.LineAlignment = StringAlignment.Center; string[] ss = e.Value.ToString().Split(";".ToCharArray()); e.Graphics.DrawString(ss[0], new Font(e.CellStyle.Font.Name, 9), new SolidBrush(e.CellStyle.ForeColor), rtYear, _sf); e.Graphics.DrawString(ss[1], new Font(e.CellStyle.Font.Name, 9), new SolidBrush(e.CellStyle.ForeColor), rtMonth, _sf); e.Graphics.DrawString(ss[2], new Font(e.CellStyle.Font.Name, 9), new SolidBrush(e.CellStyle.ForeColor), rtDay, _sf); } e.Handled = true; } #endregion #region 金额线 if ((7 <= e.ColumnIndex && 9 >= e.ColumnIndex && _currentState != UIState.ViewTotal) || (2 <= e.ColumnIndex && 5 >= e.ColumnIndex && _currentState == UIState.ViewTotal)) { int _charFullWidth = 0; int _charWidth = 0; RectangleF rtCyBound = new RectangleF(); RectangleF[] rtfs = new RectangleF[0]; Font font = grid.Columns[e.ColumnIndex].CellTemplate.Style.Font; e.Paint(e.ClipBounds, DataGridViewPaintParts.Border | DataGridViewPaintParts.Background | DataGridViewPaintParts.Focus); StringFormat _sf = new StringFormat(); _sf.Alignment = StringAlignment.Center; _sf.LineAlignment = StringAlignment.Center; if (e.RowIndex >= 0) { if (InitCyPaint(font, e.CellBounds, ref _charFullWidth, ref _charWidth, ref rtCyBound, ref rtfs)) { string s = e.FormattedValue.ToString(); s = s.Replace(".", ""); DrawTextContent(e, s, rtfs, rtCyBound, _charFullWidth, _charWidth); if (dataGridView1.SelectedColumns.Contains(dataGridView1.Columns[e.ColumnIndex]) && dataGridView1.SelectedRows.Contains(dataGridView1.Rows[e.RowIndex])) { Pen pen2 = new Pen(Color.Red, 2F); e.Graphics.DrawRectangle(pen2, e.CellBounds); } e.Handled = true; } } else { Rectangle rtCyTitle = e.CellBounds; Rectangle rtTitle = e.CellBounds; rtCyTitle.Offset(0, e.CellBounds.Height - CYTITLEHEIGHT); rtCyTitle.Height = CYTITLEHEIGHT; rtTitle.Height = e.CellBounds.Height - CYTITLEHEIGHT; if (InitCyPaint(font, rtCyTitle, ref _charFullWidth, ref _charWidth, ref rtCyBound, ref rtfs)) { e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, new SolidBrush(e.CellStyle.ForeColor), rtTitle, _sf); DrawCyTitle(e, rtfs, rtCyBound, _charFullWidth, _charWidth); e.Graphics.DrawLine(Pens.Green, e.CellBounds.Left, rtCyTitle.Top, e.CellBounds.Right - 2, rtCyTitle.Top); e.Handled = true; } } } #endregion }