Beispiel #1
2
        private void board_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            string number = "";

            // foreach(item c in List of items)
            if (_idc.Any(c => (number = c.Number) != "" && c.X == e.ColumnIndex && c.Y == e.RowIndex))
            {
                var r = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.FillRectangle(Brushes.White, r);
                var f = new Font(e.CellStyle.Font.FontFamily, 7);
                e.Graphics.DrawString(number, f, Brushes.Black, r);
                e.PaintContent(e.ClipBounds);
                e.Handled = true;
            }
        }
        public static void BuildStatusImageColumnCellPainting(DataGridViewCellPaintingEventArgs e, GitRevision revision, Brush foreBrush, Font rowFont)
        {
            if (revision.BuildStatus != null)
            {
                Image buildStatusImage = null;

                switch (revision.BuildStatus.Status)
                {
                    case BuildInfo.BuildStatus.Success:
                        buildStatusImage = Resources.BuildSuccessful;
                        break;
                    case BuildInfo.BuildStatus.Failure:
                        buildStatusImage = Resources.BuildFailed;
                        break;
                    case BuildInfo.BuildStatus.Unknown:
                        buildStatusImage = Resources.BuildCancelled;
                        break;
                    case BuildInfo.BuildStatus.InProgress:
                        buildStatusImage = Resources.Icon_77;
                        break;
                    case BuildInfo.BuildStatus.Unstable:
                        buildStatusImage = Resources.bug;
                        break;
                    case BuildInfo.BuildStatus.Stopped:
                        buildStatusImage = Resources.BuildCancelled;
                        break;
                }

                if (buildStatusImage != null)
                {
                    e.Graphics.DrawImage(buildStatusImage, new Rectangle(e.CellBounds.Left, e.CellBounds.Top + 4, 16, 16));
                }
            }
        }
        public static void BuildStatusMessageCellPainting(DataGridViewCellPaintingEventArgs e, GitRevision revision, Brush foreBrush, Font rowFont)
        {
            if (revision.BuildStatus != null)
            {
                Brush buildStatusForebrush = foreBrush;

                switch (revision.BuildStatus.Status)
                {
                    case BuildInfo.BuildStatus.Success:
                        buildStatusForebrush = Brushes.DarkGreen;
                        break;
                    case BuildInfo.BuildStatus.Failure:
                        buildStatusForebrush = Brushes.DarkRed;
                        break;
                    case BuildInfo.BuildStatus.InProgress:
                        buildStatusForebrush = Brushes.Blue;
                        break;
                    case BuildInfo.BuildStatus.Unstable:
                        buildStatusForebrush = Brushes.OrangeRed;
                        break;
                    case BuildInfo.BuildStatus.Stopped:
                        buildStatusForebrush = Brushes.Gray;
                        break;
                }

                var text = (string)e.FormattedValue;
                e.Graphics.DrawString(text, rowFont, buildStatusForebrush, new PointF(e.CellBounds.Left, e.CellBounds.Top + 4));
            }
        }
Beispiel #4
0
        private void PaintingFont(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, int cellwidth, int UpRows, int DownRows, int count)
        {
            SolidBrush fontBrush  = new SolidBrush(e.CellStyle.ForeColor);
            int        fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
            int        fontwidth  = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
            int        cellheight = e.CellBounds.Height;

            if (!string.IsNullOrEmpty(e.Value.ToString()))
            {
                if (ShowLinkValue == false)
                {
                    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
                }
                else
                {
                    DataRow[] dts = Linkvaluedt.Select(string.Format("name='{0}'", e.Value.ToString()));
                    if (dts.Length > 0)
                    {
                        if (!Convert.IsDBNull(dts[0]["name2"]))
                        {
                            fontwidth = (int)e.Graphics.MeasureString(dts[0]["name2"].ToString(), e.CellStyle.Font).Width;
                            e.Graphics.DrawString(dts[0]["name2"].ToString(), e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
                        }
                    }
                }
            }
        }
Beispiel #5
0
        // Malowanie po harmonogramu
        private void dataGridViewSchedule_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            for (int i = 2; i < dataGridViewSchedule.ColumnCount; i++)
            {
                // AutoSize kolumn
                DataGridViewColumn column = dataGridViewSchedule.Columns[i];
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader; //AllCells

                if (this.dataGridViewSchedule.Columns[i].Index ==
                    e.ColumnIndex && e.RowIndex >= 0)
                {
                    Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
                                                      e.CellBounds.Y + 1, e.CellBounds.Width - 4,
                                                      e.CellBounds.Height - 4);

                    using (
                        Brush gridBrush = new SolidBrush(this.dataGridViewSchedule.GridColor),
                        backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                    {
                        using (Pen gridLinePen = new Pen(gridBrush))
                        {
                            if (e.Value != null)
                            {
                                e.Graphics.FillRectangle(Brushes.LightGray, e.CellBounds);
                                e.PaintContent(e.CellBounds);
                                e.Graphics.DrawRectangle(Pens.Black, newRect);
                                e.Handled = true;
                            }
                        }
                    }
                }
            }
        }
Beispiel #6
0
        void DrawCyTitle(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, RectangleF[] rtfs, RectangleF rtCyTitle, int _charFullWidth, int _charWidth)
        {
            StringFormat _sf = new StringFormat();

            _sf.Alignment     = StringAlignment.Center;
            _sf.LineAlignment = StringAlignment.Center;
            Font     CyTitleFont = new Font("楷体_GB2312", 9F);
            Graphics g           = e.Graphics;

            System.Drawing.Text.TextRenderingHint      trh = g.TextRenderingHint;
            System.Drawing.Drawing2D.InterpolationMode im  = g.InterpolationMode;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            RectangleF rtf       = new RectangleF(rtCyTitle.Right - _charWidth, rtCyTitle.Y, _charWidth, rtCyTitle.Height);
            int        maxLength = rtfs.Length > CyEditor.CYTITLE.Length ? CyEditor.CYTITLE.Length : rtfs.Length;//用小的
            int        j         = 0;

            for (int i = CyEditor.CYTITLE.Length - 1; i >= 0; i--)
            {
                if (j >= maxLength)
                {
                    break;
                }
                string s = CyEditor.CYTITLE.Substring(i, 1);
                g.DrawString(s, CyTitleFont, new SolidBrush(Color.Green), rtf, _sf);
                rtf.Offset(-_charFullWidth, 0);
                j++;
            }
            g.TextRenderingHint = trh;
            g.InterpolationMode = im;
            DrawCyLine(g, rtCyTitle.X, rtCyTitle.Y, rtCyTitle.Width, rtCyTitle.Height, _charFullWidth);
        }
        private void dgvContours_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            e.Paint(e.ClipBounds, DataGridViewPaintParts.All);
            e.Handled = true;

            if (e.RowIndex < 0) return;

            Template template = samples[e.RowIndex];

            if (e.ColumnIndex == -1)
            {
                e.Graphics.DrawString(e.RowIndex.ToString(), Font, Brushes.Black, e.CellBounds.Location);
                return;
            }

            if (e.ColumnIndex == 0)
            {
                var rect = new Rectangle(e.CellBounds.X, e.CellBounds.Y, (e.CellBounds.Width - 24)/2, e.CellBounds.Height);
                rect.Inflate(-20, -20);
                Rectangle boundRect = template.contour.SourceBoundingRect;
                float k1 = 1f *  rect.Width / boundRect.Width;
                float k2 = 1f * rect.Height / boundRect.Height;
                float k = Math.Min(k1, k2);

                e.Graphics.DrawImage(bmp,
                    new Rectangle(rect.X, rect.Y, (int)(boundRect.Width * k), (int)(boundRect.Height * k)),
                    boundRect, GraphicsUnit.Pixel);
            }

            if (e.ColumnIndex == 0)
            {
                template.Draw(e.Graphics, e.CellBounds);
            }
        }
        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex != -1 && e.ColumnIndex != -1)
            {
                if ((CurrentRow != null && e.RowIndex == CurrentRow.Index) || SelectedRowsFromCells.Contains(Rows[e.RowIndex]))
                {
                    Color nextbackcolor = e.CellStyle.BackColor;
                    switch (App.CurrentConfig.Theme)
                    {
                        case Classes.Config.ThemeStyle.LessDark:
                            nextbackcolor = Color.FromArgb(116, 116, 116);
                            break;
                        case Classes.Config.ThemeStyle.Dark:
                            nextbackcolor = Color.FromArgb(84, 84, 84);
                            break;
                        case Classes.Config.ThemeStyle.MoreDark:
                            nextbackcolor = Color.FromArgb(52, 52, 52);
                            break;
                        case Classes.Config.ThemeStyle.TonOfDarkness:
                            nextbackcolor = Color.FromArgb(36, 36, 36);
                            break;
                    }
                    e.CellStyle.BackColor = nextbackcolor;
                    e.CellStyle.ForeColor = Functions.ContrastColor(e.CellStyle.BackColor);

                }
            }
            base.OnCellPainting(e);
        }
Beispiel #9
0
		void boomarkDataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
		{
			if (this.bookmarkData == null)
				return;

			lock (this.paintLock)
			{
				try
				{
					if (e.RowIndex < 0 || e.ColumnIndex < 0 || this.bookmarkData.Bookmarks.Count <= e.RowIndex)
					{
						e.Handled = false;
						return;
					}
					int lineNum = this.bookmarkData.Bookmarks[e.RowIndex].LineNum;

					//if (e.ColumnIndex == 1)
					//{
					//  CommentPainting(this.bookmarkDataGridView, lineNum, e);
					//}
					//else
					{
						PaintHelper.CellPainting(this.logPaintContext, this.bookmarkDataGridView, lineNum, e);
					}
				}
				catch (Exception ex)
				{
					Logger.logError(ex.StackTrace);
				}
			}
		}
Beispiel #10
0
        /// <summary>
        /// 画字符串
        /// </summary>
        /// <param name="e"></param>
        /// <param name="cellwidth"></param>
        /// <param name="UpRows"></param>
        /// <param name="DownRows"></param>
        /// <param name="count"></param>
        private void PaintingFont(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, int left, int up, int right, int down)
        {
            SolidBrush fontBrush  = new SolidBrush(e.CellStyle.ForeColor);
            String     content    = e.Value.ToString();
            int        fontheight = (int)e.Graphics.MeasureString(content, e.CellStyle.Font).Height;
            int        fontwidth  = (int)e.Graphics.MeasureString(content, e.CellStyle.Font).Width;
            int        cellheight = e.CellBounds.Height;
            int        cellwidth  = e.CellBounds.Width;
            int        L          = e.CellBounds.X - left * cellwidth;
            int        U          = e.CellBounds.Y - up * cellheight;
            int        R          = e.CellBounds.X + right * cellwidth;
            int        B          = e.CellBounds.Y + down * cellheight;

            int    i = 1, l = content.Length;
            String str = content;

            while (R - L < fontwidth && i < 6)
            {//字符串过长,分段
                i++;
                str = "";
                int j, m = l / i;
                for (j = 0; j < i - 1; j++)
                {
                    str += content.Substring(j * m, m) + "\n";
                }
                str       += content.Substring(j * m);
                fontheight = (int)e.Graphics.MeasureString(str, e.CellStyle.Font).Height;
                fontwidth  = (int)e.Graphics.MeasureString(str, e.CellStyle.Font).Width;
            }

            e.Graphics.DrawString(str, e.CellStyle.Font, fontBrush, L + (R - L - fontwidth) / 2, U + (B - U - fontheight) / 2);
        }
        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            base.OnCellPainting(e);

            if (e.RowIndex >= 0 && Rows[e.RowIndex].Tag is Host)
            {
                PoolHostDataGridViewOneCheckboxRow row = (PoolHostDataGridViewOneCheckboxRow)Rows[e.RowIndex];
                //Paintout checkbox - note: it's still there
                if (!row.CheckBoxVisible && e.ColumnIndex == 1)
                {
                    e.PaintBackground(e.ClipBounds, true);
                    e.Handled = true;
                }
                else if (row.HasPool && (e.ColumnIndex == 0 || e.ColumnIndex == 1))
                {
                    e.PaintBackground(e.ClipBounds, true);
                    e.Handled = true;
                }
                else if (!row.HasPool && e.ColumnIndex == 0)
                {
                    e.PaintBackground(e.ClipBounds, true);
                    e.Handled = true;
                }
            }
        }
Beispiel #12
0
    /// <summary>
    /// 画字符串
    /// </summary>
    /// <param name="e"></param>
    /// <param name="cellwidth"></param>
    /// <param name="UpRows"></param>
    /// <param name="DownRows"></param>
    /// <param name="count"></param>
    private void PaintingFont(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, int cellwidth, int UpRows, int DownRows, int count)
    {
        SolidBrush fontBrush  = new SolidBrush(e.CellStyle.ForeColor);
        int        fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
        int        fontwidth  = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
        int        cellheight = e.CellBounds.Height;

        e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
    }
        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            Rectangle newRect = new Rectangle(e.CellBounds.X - 1, e.CellBounds.Y - 1, e.CellBounds.Width, e.CellBounds.Height);

            if (e.ColumnIndex == -1 && e.RowIndex == -1)
            {
                e.Graphics.DrawRectangle(Pens.LightSteelBlue, newRect);
            }
            else
            {
                if (e.ColumnIndex == -1)
                {
                    Color begin = Color.FromArgb(241, 244, 248);
                    Color end = RowHeaderColor;
                    using (Brush brush = new LinearGradientBrush(
                        e.CellBounds, begin, end, LinearGradientMode.Vertical))
                    {
                        e.Graphics.FillRectangle(brush, e.CellBounds);
                        e.Graphics.DrawRectangle(Pens.LightSteelBlue, newRect);
                    }

                    DataGridViewPaintParts pa =
                        DataGridViewPaintParts.Border |
                        DataGridViewPaintParts.ContentBackground |
                        DataGridViewPaintParts.ContentForeground |
                        DataGridViewPaintParts.ErrorIcon |
                        DataGridViewPaintParts.Focus |
                        DataGridViewPaintParts.SelectionBackground;
                    e.Paint(e.ClipBounds, pa);

                    e.Handled = true;
                }
                if (e.RowIndex == -1)
                {
                    Color begin = Color.FromArgb(241, 244, 248);
                    Color end = ColumnHeaderColor;

                    using (Brush brush = new LinearGradientBrush(
                        e.CellBounds, begin, end, LinearGradientMode.Vertical))
                    {
                        e.Graphics.FillRectangle(brush, e.CellBounds);
                        e.Graphics.DrawRectangle(Pens.LightSteelBlue, newRect);
                    }

                    DataGridViewPaintParts pa =
                        DataGridViewPaintParts.Border |
                        DataGridViewPaintParts.ContentBackground |
                        DataGridViewPaintParts.ContentForeground |
                        DataGridViewPaintParts.ErrorIcon |
                        DataGridViewPaintParts.Focus |
                        DataGridViewPaintParts.SelectionBackground;
                    e.Paint(e.ClipBounds, pa);

                    e.Handled = true;
                }
            }
        }
Beispiel #14
0
        private void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            // check that we are in a header cell!
            if (e.RowIndex == -1 && e.ColumnIndex > 0)
            {
                e.PaintBackground(e.ClipBounds, true);
                Rectangle rect = MatrixDataGridView.GetColumnDisplayRectangle(e.ColumnIndex, true);
                Size titleSize = TextRenderer.MeasureText(e.Value.ToString(), e.CellStyle.Font);
                int offset = 4;
                if (MatrixDataGridView.ColumnHeadersHeight < titleSize.Width + 2 * offset)
                {
                    MatrixDataGridView.ColumnHeadersHeight = titleSize.Width + 2 * offset;
                }

                e.Graphics.TranslateTransform(1, titleSize.Width);
                e.Graphics.RotateTransform(-90.0f);

                e.Graphics.DrawString(e.Value.ToString(), MatrixDataGridView.Font, Brushes.Black,
                    new PointF(rect.Y - (MatrixDataGridView.ColumnHeadersHeight - titleSize.Width - offset), rect.X));

                e.Graphics.RotateTransform(90.0f);
                e.Graphics.TranslateTransform(-1, -titleSize.Width);

                e.Handled = true;
            }

            if (e.RowIndex != -1 && e.ColumnIndex > 0)
            {
                var state = (ProjectDependency)e.Value;
                /*if (state == ProjectDependency.Invalid)
                    e.CellStyle.BackColor = Color.Silver;*/

                e.PaintBackground(e.ClipBounds, true);

                Rectangle rect = MatrixDataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);

                var CheckBoxRegion = new Rectangle(rect.X + box_offset, rect.Y + box_offset, box_size, box_size);
                switch (state)
                {
                    case ProjectDependency.None:
                        ControlPaint.DrawCheckBox(e.Graphics, CheckBoxRegion, ButtonState.Normal);
                        break;
                    case ProjectDependency.Direct:
                        ControlPaint.DrawCheckBox(e.Graphics, CheckBoxRegion, ButtonState.Checked);
                        break;
                    case ProjectDependency.Indirect:
                        ControlPaint.DrawCheckBox(e.Graphics, CheckBoxRegion, ButtonState.Checked | ButtonState.Inactive);
                        break;
                    case ProjectDependency.Invalid:
                        ControlPaint.DrawCheckBox(e.Graphics, CheckBoxRegion, ButtonState.Inactive);
                        break;
                }

                e.Handled = true;
            }
        }
        private void dgKarton_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            //if ((e.RowIndex != -1) && (e.ColumnIndex == dgKarton.Columns["CIKK_ALL_KESZLET"].Index))
            //{
            //    if ((double)dgKarton.Rows[e.RowIndex].Cells[dgKarton.Columns["CIKK_ALL_KESZLET"].Index].Value < (double)dgKarton.Rows[e.RowIndex].Cells[dgKarton.Columns["OPTIMALIS_KESZLET"].Index].Value)
            //    {
            //        dgKarton.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Yellow;
            //        //dgKarton.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Yellow;
            //    }
            //    if ((double)dgKarton.Rows[e.RowIndex].Cells[dgKarton.Columns["CIKK_ALL_KESZLET"].Index].Value < (double)dgKarton.Rows[e.RowIndex].Cells[dgKarton.Columns["MINIMUM_KESZLET"].Index].Value)
            //    {
            //        dgKarton.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Red;
            //        //dgKarton.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
            //    }

            //}
            if (this.dgKarton.Columns["CIKK_ALL_KESZLET"].Index ==
                   e.ColumnIndex && e.RowIndex >= 0)
            {
                Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
                    e.CellBounds.Y + 1, e.CellBounds.Width - 4,
                    e.CellBounds.Height - 4);

                using (
                    Brush gridBrush = new SolidBrush(this.dgKarton.GridColor),
                    backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                {
                    using (Pen gridLinePen = new Pen(gridBrush))
                    {
                        // Erase the cell.
                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                        // Draw the grid lines (only the right and bottom lines;
                        // DataGridView takes care of the others).
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                            e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                            e.CellBounds.Bottom - 1);
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                            e.CellBounds.Top, e.CellBounds.Right - 1,
                            e.CellBounds.Bottom);

                        // Draw the inset highlight box.
                       // e.Graphics.DrawRectangle(Pens.Blue, newRect);

                        // Draw the text content of the cell, ignoring alignment.
                        if (e.Value != null)
                        {
                            e.Graphics.DrawString((String)e.Value.ToString(), e.CellStyle.Font,
                                Brushes.Crimson, e.CellBounds.X + 2,
                                e.CellBounds.Y + 2, StringFormat.GenericDefault);
                        }
                        e.Handled = true;
                    }
                }
            }
        }
Beispiel #16
0
 private void playlistView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
 {
     if (e.RowIndex >= 0 && e.RowIndex < mpc.CurrentPlaylist.Count)
     {
         if (mpc.CurrentPlaylist[e.RowIndex].ID == mpc.CurrentSong.ID)
         {
             e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold);
         }
     }
 }
Beispiel #17
0
 private void dgvClient_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
 {
     //if (e.ColumnIndex == 3 && e.RowIndex > -1)
     //{
     //    Image img = GymMgr.Properties.Resources.add;
     //    e.Graphics.DrawImage(img, e.CellBounds.Location);
     //    e.PaintContent(e.CellBounds);
     //    e.Handled = true;
     //}
 }
        private static void SetCellBounds(DataGridViewCellPaintingEventArgs e, Rectangle bounds)
        {
            // HACK: this works around the fact that PaintContent for column headers
            // ingore the clipbounds passed into it, instead using CellBounds, and the
            // fact that CellBounds is a readonly property.  It relies on the implementation
            // detail that CellBounds has a backing private field called cellBounds.  This may
            // break on future updates to the framework.

            typeof(DataGridViewCellPaintingEventArgs).InvokeMember("cellBounds", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.SetField, null, e, new object[]{bounds});
        }
Beispiel #19
0
 protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
 {
     base.OnCellPainting(e);
     Point p = new Point(e.ColumnIndex, e.RowIndex);
     if (invisibleColumnRow.Contains(p))
     {
         e.Graphics.FillRectangle(new SolidBrush(this.BackgroundColor), e.CellBounds);
         e.Handled = true;
     }
 }
Beispiel #20
0
        //セル描画イベントハンドラ
        private void DataGridViewEx_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            //このメソッドでは、ヘッダの描画のみを行う
            if (e.ColumnIndex >= 0 && e.RowIndex == -1)
            {
                //そのヘッダがどのドッキングセルに属しているか調べる
                int index = -1;
                for (int i = 0; i < DockingCellList.Count; i++)
                {
                    if (e.ColumnIndex >= DockingCellList[i].Start
                        && e.ColumnIndex < DockingCellList[i].Start + DockingCellList[i].Count)
                    {
                        index = i;
                        break;
                    }
                }

                //とりあえず塗りつぶす
                e.Graphics.FillRectangle(new SolidBrush(e.CellStyle.BackColor), e.CellBounds);

                //ドッキングしている場合
                if (index >= 0)
                {
                    //上段
                    int x = e.CellBounds.X;
                    for (int i = e.ColumnIndex - 1; i >= DockingCellList[index].Start; i--)
                    {
                        x -= this.Columns[i].Width;
                    }
                    int width = 0;
                    for (int i = DockingCellList[index].Start; i < DockingCellList[index].Start + DockingCellList[index].Count; i++)
                    {
                        width += this.Columns[i].Width;
                    }
                    Rectangle rect1 = new Rectangle(x, e.CellBounds.Y, width, e.CellBounds.Height / 2);
                    DrawCell(e.Graphics, rect1);
                    e.Graphics.DrawString(DockingCellList[index].Text, e.CellStyle.Font, Brushes.Black, rect1.X + 2, rect1.Y + 2);

                    //下段
                    Rectangle rect2 = new Rectangle(e.CellBounds.X, e.CellBounds.Y + e.CellBounds.Height / 2, e.CellBounds.Width, e.CellBounds.Height / 2);
                    DrawCell(e.Graphics, rect2);
                    e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, rect2.X + 2, rect2.Y + 2);
                }

                //ドッキングしていない場合
                else
                {
                    DrawCell(e.Graphics, e.CellBounds);
                    e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 2);
                }

                //ハンドルされたことを報告
                e.Handled = true;
            }
        }
Beispiel #21
0
 protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
 {
     base.OnCellPainting(e);
     if (e.ColumnIndex > -1 && e.RowIndex > -1)
     {
         if (this[e.ColumnIndex, e.RowIndex].Value == null || this[e.ColumnIndex, e.RowIndex].Value.Equals(""))
             this[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Transparent;
         else
             this[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.CornflowerBlue;
     }
 }
Beispiel #22
0
 private void gvEmptyNetworkName_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
 {
     if (e.RowIndex < 0)
         return;
     if (e.ColumnIndex == dcSelect.Index)
     {
         e.Paint(e.CellBounds, DataGridViewPaintParts.All);
         e.Graphics.DrawImage(Resources.Select, e.CellBounds.Left + 3, e.CellBounds.Top + 3);
         e.Handled = true;
     }
 }
Beispiel #23
0
        void dataGridView_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex < 0)
            {
                return;
            }
            if (e.RowIndex == 0)
            {
                return;
            }
            //use default painting for first selected cell.
            if (dataGridView.SelectedCells.Count > 0)
            {
                var selectedCell = dataGridView.SelectedCells[0];
                if (selectedCell.RowIndex == e.RowIndex && selectedCell.ColumnIndex == e.ColumnIndex)
                {
                    return;
                }
            }
            System.Drawing.Rectangle newRect = new System.Drawing.Rectangle(e.CellBounds.X + 1,
                                                                            e.CellBounds.Y + 1, e.CellBounds.Width - 4,
                                                                            e.CellBounds.Height - 4);

            using (System.Drawing.Brush gridBrush = new System.Drawing.SolidBrush(this.dataGridView.GridColor),
                   backColorBrush = new System.Drawing.SolidBrush(e.CellStyle.BackColor))
            {
                using (System.Drawing.Pen gridLinePen = new System.Drawing.Pen(gridBrush))
                {
                    // Erase the cell.
                    e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                    // Draw the grid lines (only the right and bottom lines;
                    // DataGridView takes care of the others).
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                                        e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                                        e.CellBounds.Bottom - 1);
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                                        e.CellBounds.Top, e.CellBounds.Right - 1,
                                        e.CellBounds.Bottom);

                    // Draw the inset highlight box.


                    //Draw the text content of the cell, ignoring alignment.
                    if (e.Value != null)
                    {
                        e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                                              System.Drawing.Brushes.Black, e.CellBounds.X + 2,
                                              e.CellBounds.Y + 2, System.Drawing.StringFormat.GenericDefault);
                    }
                    e.Handled = true;
                }
            }
        }
Beispiel #24
0
        /// <summary>
        /// 画指定值的字符串
        /// </summary>
        /// <param name="e"></param>
        /// <param name="cellwidth"></param>
        /// <param name="UpRows"></param>
        /// <param name="DownRows"></param>
        /// <param name="count"></param>
        private void PaintingFont(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, string Value, RectangleF rectDest)
        {
            SolidBrush   fontBrush = new SolidBrush(e.CellStyle.ForeColor);
            StringFormat sf        = new StringFormat();

            sf.Alignment     = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            sf.Trimming      = StringTrimming.EllipsisCharacter;

            e.Graphics.DrawString(Value, e.CellStyle.Font, fontBrush, rectDest, sf);
        }
 /// <summary>
 /// add warning flag ahead of the row if it is not up to date
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 /// <remarks></remarks>
 private void DataGV_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
 {
     if (e.ColumnIndex < 0 && e.RowIndex >= 0 && e.RowIndex < DataGV.Rows.Count)
     {
         if (DataGV.Rows[e.RowIndex].Cells["Status"].Value != "Up-to-date")
         {
             e.PaintBackground(e.ClipBounds, true);
             e.Graphics.DrawImage(Resources.Light_Bulb.ToBitmap(), e.CellBounds);
             e.Handled = true;
         }
     }
 }
		private void upgradeEquipmentGrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
		{
			// This just hides numeric fields that aren't supported, they return -1
			if ((e.Value is int && (int)e.Value == -1) ||
				(e.Value is double && Math.Abs((double)e.Value + 1) < Double.Epsilon) ||
				(e.Value is EquippableSlotFlags && (EquippableSlotFlags)e.Value == EquippableSlotFlags.None) ||
				(e.Value is CoverageFlags && (CoverageFlags)e.Value == CoverageFlags.None))
			{
				e.PaintBackground(e.ClipBounds, true);
				e.Handled = true;
			}
		}
Beispiel #27
0
 private void AreasTree_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
 {
     if (e.RowIndex < 0)
         return;
     if (e.ColumnIndex == dcSelectToReport.Index)
     {
         Image cellImage = Resources.print;
         e.Paint(e.CellBounds, DataGridViewPaintParts.All);
         e.Graphics.DrawImage(cellImage, e.CellBounds.Left + 3, e.CellBounds.Top + 3);
         e.Handled = true;
     }
 }
        private void OnGridCellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }

            if (this.m_Grid.Columns[e.ColumnIndex].Name == Strings.DATASHEET_COLOR_COLUMN_NAME)
            {
                ColorColumns.ColorPaintGridCell(this.m_Grid, e);
            }
        }
        private void dgCategories_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if ((decimal)dsCategories.Tables["Category"].Rows[e.RowIndex]["total"] < 0)
                    e.CellStyle.BackColor = Settings.Default.NegativeTotalRowColor;

                if ((decimal)dsCategories.Tables["Category"].Rows[e.RowIndex]["total"] > 0)
                    e.CellStyle.BackColor = Settings.Default.PositiveTotalRowColor;

            }
        }
        private void DrawStatusCell(DataGridViewCellPaintingEventArgs e)
        {
            var sys = currentSystemList[e.RowIndex];
            Brush brush;
            if (sys.Alive)
            {
                //e.Value = global::AutosysMonitor.Properties.Resources.Red_ball;
                brush = Brushes.Green;
            }
            else
            {
                brush = Brushes.Red;
            }

            using (
                    Brush gridBrush = new SolidBrush(this.SystemView.GridColor),
                    backColorBrush = new SolidBrush(e.CellStyle.BackColor))
            {
                if (sys.GetType() == typeof(LineSplitter))
                {
                    brush = backColorBrush;
                }
                using (Pen gridLinePen = new Pen(gridBrush))
                {
                    // Erase the cell.
                    e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                    // Draw the grid lines (only the right and bottom lines;
                    // DataGridView takes care of the others).
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                        e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                        e.CellBounds.Bottom - 1);
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                        e.CellBounds.Top, e.CellBounds.Right - 1,
                        e.CellBounds.Bottom);

                    //Draw image
                    int horsMiddle = (e.CellBounds.Left + e.CellBounds.Right) / 2;
                    int vertMiddle = (e.CellBounds.Bottom + e.CellBounds.Top) / 2;
                    var diameter = Math.Min(e.CellBounds.Width, e.CellBounds.Height);
                    diameter -= 4;
                    horsMiddle -= (diameter / 2);
                    vertMiddle -= (diameter / 2);

                    Rectangle bounds = new Rectangle(horsMiddle, vertMiddle, diameter, diameter);

                    e.Graphics.FillEllipse(brush, bounds);

                    e.Handled = true;
                }
            }
        }
Beispiel #31
0
 protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
 {
     if (e.ColumnIndex == 1)
     {
         e.Paint(e.ClipBounds, DataGridViewPaintParts.Background | DataGridViewPaintParts.Border);
         DrawGanttBar(e, e.RowIndex);
         e.Handled = true;
     }
     else
     {
         base.OnCellPainting(e);
     }
 }
 private void BlameGrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
 {
     if (e.RowIndex >= 0 && (e.State & DataGridViewElementStates.Visible) != 0)
     {
         if (e.ColumnIndex == 0)
         {
             e.Handled = true;
             GitBlame blame = ((GitBlame)BlameGrid.Rows[e.RowIndex].DataBoundItem);
             e.Graphics.FillRectangle(new SolidBrush(blame.color), e.CellBounds);
             e.Graphics.DrawString(blame.Author, BlameGrid.Font, new SolidBrush(Color.Black), new PointF(e.CellBounds.Left, e.CellBounds.Top + 4));
         }
     }
 }
 private void DataGridView_CellPaintingRotate(object sender, DataGridViewCellPaintingEventArgs e)
 {
     // Vertical text from column 0, or adjust below, if first column(s) to be skipped
     if (e.RowIndex == -1 && e.ColumnIndex > 1)
     {
         e.PaintBackground(e.CellBounds, true);
         e.Graphics.TranslateTransform(e.CellBounds.Left, e.CellBounds.Bottom);
         e.Graphics.RotateTransform(270);
         e.Graphics.DrawString(e.FormattedValue.ToString(), e.CellStyle.Font, Brushes.Black, 5, 5);
         e.Graphics.ResetTransform();
         e.Handled = true;
     }
 }
Beispiel #34
0
        public void grd_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e, DataGridView grd, System.Drawing.Font font)
        {
            StringFormat StrF = new StringFormat();

            StrF.Alignment = StringAlignment.Center;

            if (e.ColumnIndex == -1 && e.RowIndex >= 0 && e.RowIndex < grd.Rows.Count)
            {
                e.PaintBackground(e.ClipBounds, true);
                e.Graphics.DrawString((e.RowIndex + 1).ToString(), font, Brushes.Black, e.CellBounds, StringFormat.GenericTypographic);
                e.Handled = true;
            }
        }
Beispiel #35
0
        private void DataGridView1_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            //'列ヘッダーかどうか調べる
            //If e.ColumnIndex < 0 And e.RowIndex >= 0 Then
            //    'セルを描画する
            //    e.Paint(e.ClipBounds, DataGridViewPaintParts.All)

            //    '行番号を描画する範囲を決定する
            //    'e.AdvancedBorderStyleやe.CellStyle.Paddingは無視しています
            //    Dim indexRect As Rectangle = e.CellBounds
            //    indexRect.Inflate(-2, -2)
            //    '行番号を描画する
            //    TextRenderer.DrawText(e.Graphics, _
            //        (e.RowIndex + 1).ToString(), _
            //        e.CellStyle.Font, _
            //        indexRect, _
            //        e.CellStyle.ForeColor, _
            //        TextFormatFlags.Right Or TextFormatFlags.VerticalCenter)
            //    '描画が完了したことを知らせる
            //    e.Handled = True
            //End If

            //'ヘッダー以外のセルで、背景を描画する時
            //If e.ColumnIndex = 0 AndAlso e.RowIndex = 0 AndAlso _
            //    (e.PaintParts And DataGridViewPaintParts.Background) = _
            //        DataGridViewPaintParts.Background Then
            //    '背景だけを描画する
            //    Dim backParts As DataGridViewPaintParts = _
            //        e.PaintParts And (DataGridViewPaintParts.Background Or _
            //            DataGridViewPaintParts.SelectionBackground)
            //    e.Paint(e.ClipBounds, backParts)

            //    '画像をセルの範囲いっぱいに表示する
            //    'e.Graphics.DrawImage(cellBackImage, e.CellBounds)
            //    e.Graphics.DrawImage(cellBackImage, e.CellBounds.Left, e.CellBounds.Top, cellBackImage.Width, cellBackImage.Height)

            //    DataGridView1.Columns(0).MinimumWidth = cellBackImage.Width
            //    DataGridView1.Rows(e.RowIndex).MinimumHeight = cellBackImage.Height



            //    '背景以外が描画されるようにする
            //    Dim paintParts As DataGridViewPaintParts = _
            //        e.PaintParts And Not backParts
            //    'セルを描画する
            //    e.Paint(e.ClipBounds, paintParts)

            //    '描画が完了したことを知らせる
            //    e.Handled = True
            //End If
        }
Beispiel #36
0
        private void CyEdit_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            const int       CYTITLEHEIGHT = 25;
            VchDataGridView grid          = sender as VchDataGridView;

            #region 金额线
            if ((grid.Columns[e.ColumnIndex] is CyEditorTextBoxColumn))
            {
                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 (grid.SelectedColumns.Contains(grid.Columns[e.ColumnIndex]) && grid.SelectedRows.Contains(grid.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
        }
Beispiel #37
0
        /// <summary>
        /// 画字符串
        /// </summary>
        /// <param name="e"></param>
        /// <param name="cellwidth"></param>
        /// <param name="UpRows"></param>
        /// <param name="DownRows"></param>
        /// <param name="count"></param>
        private void PaintingFont(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, int cellwidth, int UpRows, int DownRows, int count)
        {
            SolidBrush fontBrush  = new SolidBrush(e.CellStyle.ForeColor);
            int        fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
            int        fontwidth  = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
            int        cellheight = e.CellBounds.Height;

            //这里留意,可能前台有格式化过,所以这里画格式化的值
            if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomCenter)
            {
                e.Graphics.DrawString((String)e.FormattedValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y + cellheight * DownRows - fontheight);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomLeft)
            {
                e.Graphics.DrawString((String)e.FormattedValue, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y + cellheight * DownRows - fontheight);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomRight)
            {
                e.Graphics.DrawString((String)e.FormattedValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y + cellheight * DownRows - fontheight);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleCenter)
            {
                e.Graphics.DrawString((String)e.FormattedValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleLeft)
            {
                e.Graphics.DrawString((String)e.FormattedValue, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleRight)
            {
                e.Graphics.DrawString((String)e.FormattedValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopCenter)
            {
                e.Graphics.DrawString((String)e.FormattedValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1));
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopLeft)
            {
                e.Graphics.DrawString((String)e.FormattedValue, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1));
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopRight)
            {
                e.Graphics.DrawString((String)e.FormattedValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1));
            }
            else
            {
                e.Graphics.DrawString((String)e.FormattedValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
            }
        }
Beispiel #38
0
 protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
 {
     //如果是Column
     if (e.RowIndex == -1)
     {
         drawColumnAndRow(e);
         e.Handled = true;
         //如果是Rowheader
     }
     else if (e.ColumnIndex < 0 && e.RowIndex >= 0)
     {
         drawColumnAndRow(e);
         e.Handled = true;
     }
 }
Beispiel #39
0
        /// <summary>
        /// 不是初次单元格绘制
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="cellArgs"></param>
        /// <param name="minColIndex"></param>
        /// <param name="maxColIndex"></param>
        public static void IsPostMerage(DataGridView dgv, DataGridViewCellPaintingEventArgs cellArgs, int minColIndex, int maxColIndex)
        {
            //比较单元是否有变化
            Rectangle rectArgs = (Rectangle)rowSpan[cellArgs.ColumnIndex];
            if (rectArgs.X != cellArgs.CellBounds.X || rectArgs.Y != cellArgs.CellBounds.Y
                || rectArgs.Width != cellArgs.CellBounds.Width || rectArgs.Height != cellArgs.CellBounds.Height)
            {
                rectArgs.X = cellArgs.CellBounds.X;
                rectArgs.Y = cellArgs.CellBounds.Y;
                rectArgs.Width = cellArgs.CellBounds.Width;
                rectArgs.Height = cellArgs.CellBounds.Height;
                rowSpan[cellArgs.ColumnIndex] = rectArgs;
            }
            MeragePrint(dgv, cellArgs, minColIndex, maxColIndex);

        }
        /// <summary>
        /// Repaint an image cell with an image in an image
        /// list. Run this method in the CellPainting event
        /// of the DataGridView.
        /// 
        /// This method ignores invalid parameters. Provide
        /// it with empty or invalid indices to cause it to
        /// abort.
        /// </summary>
        public static void PaintImageCell(this DataGridView dataGridView, DataGridViewCellPaintingEventArgs e, ImageList imageList, String imageKey)
        {
            //Abort if any parameter is null or if the image does not exist
            if (e == null || imageList == null || imageKey.IsNullOrEmpty())
                return;
            if (!imageList.Images.ContainsKey(imageKey))
                return;

            //Paint the image
            e.PaintBackground(e.ClipBounds, false);
            var pt = e.CellBounds.Location;
            pt.X += (e.CellBounds.Width - imageList.ImageSize.Width) / 2;
            pt.Y += 4;
            imageList.Draw(e.Graphics, pt, imageList.Images.IndexOfKey(imageKey));
            e.Handled = true;
        }
Beispiel #41
0
        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            Icon icon = (e.Value as Icon);

            if (null != icon)
            {
                using (SolidBrush b = new SolidBrush(e.CellStyle.BackColor))
                {
                    e.Graphics.FillRectangle(b, e.CellBounds);
                }

                // Draw right aligned icon (1 pixed padding)
                e.Graphics.DrawIcon(icon, e.CellBounds.Width - icon.Width - 1, e.CellBounds.Y + 1);
                e.Handled = true;
            }
        }
Beispiel #42
0
 void drawColumnAndRow(DataGridViewCellPaintingEventArgs e)
 {
     // 绘制背景色
     using (LinearGradientBrush backbrush = new LinearGradientBrush(e.CellBounds, Color.Orange, Color.Orange, LinearGradientMode.Vertical))
     {
         Rectangle border = e.CellBounds;
         border.Width -= 1;
         //填充绘制效果
         e.Graphics.FillRectangle(backbrush, border);
         //绘制Column、Row的Text信息
         e.PaintContent(e.CellBounds);
         //绘制边框
         //ControlPaint.DrawBorder3D(e.Graphics, e.CellBounds, Border3DStyle.Flat);
         ControlPaint.DrawBorder(e.Graphics, e.CellBounds, Color.White, ButtonBorderStyle.Solid);
     }
 }
		void OnGridCellPainting(object sender, DataGridViewCellPaintingEventArgs e)
		{
			if (e.RowIndex == -1 && e.ColumnIndex == checkBoxColumn.Index)
			{
	            var headerRectangle = grid.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);

				var point = new Point
				            	{
				            		X = headerRectangle.Location.X + (headerRectangle.Width - headerCheckBox.Width) / 2 + 1,
				            		Y = headerRectangle.Location.Y + (headerRectangle.Height - headerCheckBox.Height) / 2 + 1
				            	};


				headerCheckBox.Location = point;
			}
		}
Beispiel #44
0
        void DrawTextContent(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, string ss, RectangleF[] rtfs, RectangleF rtCyText, int _charFullWidth, int _charWidth)
        {
            Graphics g      = e.Graphics;
            bool     _isSub = false;

            if (ss.Length > 0)
            {
                if (double.Parse(ss) != 0)
                {
                    StringFormat _sf = new StringFormat();
                    _sf.Alignment     = StringAlignment.Center;
                    _sf.LineAlignment = StringAlignment.Center;

                    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
                    //这里和标题不一样
                    RectangleF rtf = new RectangleF(rtCyText.Right - _charFullWidth, rtCyText.Y, _charWidth, rtCyText.Height);
                    Brush      b;
                    Brush      _bRed   = new SolidBrush(Color.Red);
                    Brush      _bBlack = new SolidBrush(Color.Black);
                    _isSub = ss.Substring(0, 1) == "-" ? true : false;

                    if (_isSub)
                    {
                        ss = ss.Substring(1);
                        b  = _bRed;
                    }
                    else
                    {
                        b = _bBlack;
                    }
                    if (ss.Length > rtfs.Length)
                    {
                        ss = new string('#', rtfs.Length);
                    }

                    for (int i = ss.Length - 1; i >= 0; i--)
                    {
                        string s = ss.Substring(i, 1);
                        g.DrawString(s, e.CellStyle.Font, b, rtf, _sf);
                        rtf.Offset(-_charFullWidth, 0);
                    }
                }
            }
            DrawCyLine(g, rtCyText.X, rtCyText.Y, rtCyText.Width, rtCyText.Height, _charFullWidth);
        }
Beispiel #45
0
    ///<summary>
    ///绘制合并表头
    ///</summary>
    ///<param name="node">合并表头节点</param>
    ///<param name="e">绘图参数集</param>
    ///<param name="level">结点深度</param>
    ///<remarks></remarks>
    public void PaintUnitHeader(TreeNode node, System.Windows.Forms.DataGridViewCellPaintingEventArgs e, int level)
    {
        //根节点时退出递归调用
        if (level == 0)
        {
            return;
        }
        RectangleF   uhRectangle;
        int          uhWidth;
        SolidBrush   gridBrush      = new SolidBrush(this.GridColor);
        SolidBrush   backColorBrush = new SolidBrush(e.CellStyle.BackColor);
        Pen          gridLinePen    = new Pen(gridBrush);
        StringFormat textFormat     = new StringFormat();

        textFormat.Alignment = StringAlignment.Center;
        uhWidth = GetUnitHeaderWidth(node);
        if (node.Nodes.Count == 0)
        {
            uhRectangle = new Rectangle(e.CellBounds.Left, e.CellBounds.Top + node.Level * _cellHeight, uhWidth - 1,
                                        _cellHeight * (_columnDeep - node.Level) - 1);
        }
        else
        {
            uhRectangle = new Rectangle(e.CellBounds.Left, e.CellBounds.Top + node.Level * _cellHeight, uhWidth - 1,
                                        _cellHeight - 1);
        }
        //画矩形
        e.Graphics.FillRectangle(backColorBrush, uhRectangle);
        //划底线
        e.Graphics.DrawLine(gridLinePen, uhRectangle.Left, uhRectangle.Bottom, uhRectangle.Right, uhRectangle.Bottom);
        //划右端线
        e.Graphics.DrawLine(gridLinePen, uhRectangle.Right, uhRectangle.Top, uhRectangle.Right, uhRectangle.Bottom);
        ////写字段文本
        e.Graphics.DrawString(node.Text, this.Font, new SolidBrush(e.CellStyle.ForeColor),
                              uhRectangle.Left + uhRectangle.Width / 2 - e.Graphics.MeasureString(node.Text, this.Font).Width / 2 - 1,
                              uhRectangle.Top + uhRectangle.Height / 2 - e.Graphics.MeasureString(node.Text, this.Font).Height / 2);
        //递归调用()
        if (node.PrevNode == null)
        {
            if (node.Parent != null)
            {
                PaintUnitHeader(node.Parent, e, level - 1);
            }
        }
    }
Beispiel #46
0
        private void dataGridView1_CellPainting(object sender,
                                                System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            if (this.dataGridView1.Columns["ContactName"].Index ==
                e.ColumnIndex && e.RowIndex >= 0)
            {
                Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
                                                  e.CellBounds.Y + 1, e.CellBounds.Width - 4,
                                                  e.CellBounds.Height - 4);

                using (
                    Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
                    backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                {
                    using (Pen gridLinePen = new Pen(gridBrush))
                    {
                        // Erase the cell.
                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                        // Draw the grid lines (only the right and bottom lines;
                        // DataGridView takes care of the others).
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                                            e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                                            e.CellBounds.Bottom - 1);
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                                            e.CellBounds.Top, e.CellBounds.Right - 1,
                                            e.CellBounds.Bottom);

                        // Draw the inset highlight box.
                        e.Graphics.DrawRectangle(Pens.Blue, newRect);

                        // Draw the text content of the cell, ignoring alignment.
                        if (e.Value != null)
                        {
                            e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                                                  Brushes.Crimson, e.CellBounds.X + 2,
                                                  e.CellBounds.Y + 2, StringFormat.GenericDefault);
                        }
                        e.Handled = true;
                    }
                }
            }
        }
Beispiel #47
0
        /// <summary>
        /// Draws the focus rectangle
        /// </summary>
        private void DataGridViewMW_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            if (this.CurrentCell == null)
            {
                return;
            }
            if (e.ColumnIndex == this.CurrentCell.ColumnIndex && e.RowIndex == this.CurrentCell.RowIndex)
            {
                e.Paint(e.CellBounds, DataGridViewPaintParts.All);

                using (Pen p = new Pen(Color.Black, 4))
                {
                    Rectangle rect = e.CellBounds;
                    rect.Width  -= 1;
                    rect.Height -= 1;
                    ControlPaint.DrawFocusRectangle(e.Graphics, rect);
                }
                e.Handled = true;
            }
        }
        /// <summary>
        /// 单元格绘制(重写)
        /// </summary>
        /// <param name="e"></param>
        /// <remarks></remarks>
        protected override void OnCellPainting(System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex == -1 || lstLeafColumn == null || lstLeafColumn.Count <= 0)
            {
                base.OnCellPainting(e);
                return;
            }

            //列标题重写
            if (e.RowIndex == -1 && e.ColumnIndex >= 0 && e.ColumnIndex < lstLeafColumn.Count)
            {
                PaintUnitHeader(lstLeafColumn[e.ColumnIndex], e);
                e.Handled = true;
            }
            else
            {
                //需要写,否则以后使用控件时不能触发单元格的刷新事件
                base.OnCellPainting(e);
            }
        }
Beispiel #49
0
        public static void ColorPaintGridCell(DataGridView gridView, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            Color clr = Color.White;
            DataGridViewTextBoxCell cell = (DataGridViewTextBoxCell)(gridView.Rows[e.RowIndex].Cells[e.ColumnIndex]);

            if (cell.Value != null && cell.Value != DBNull.Value)
            {
                clr = ColorUtilities.ColorFromString(Convert.ToString(cell.Value, CultureInfo.InvariantCulture));
            }

            if (gridView.Rows[e.RowIndex].Selected)
            {
                clr = gridView.DefaultCellStyle.SelectionBackColor;
            }

            using (SolidBrush b = new SolidBrush(clr))
            {
                e.Graphics.FillRectangle(b, e.CellBounds);
            }

            using (Pen p = new Pen(gridView.GridColor))
            {
                e.Graphics.DrawLine(p, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                e.Graphics.DrawLine(p, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);

                if (cell == gridView.CurrentCell)
                {
                    using (Pen p2 = new Pen(Color.Black))
                    {
                        p2.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                        e.Graphics.DrawLine(p2, e.CellBounds.Left + 2, e.CellBounds.Top + 2, e.CellBounds.Right - 4, e.CellBounds.Top + 2);
                        e.Graphics.DrawLine(p2, e.CellBounds.Right - 4, e.CellBounds.Top + 2, e.CellBounds.Right - 4, e.CellBounds.Bottom - 4);
                        e.Graphics.DrawLine(p2, e.CellBounds.Right - 4, e.CellBounds.Bottom - 4, e.CellBounds.Left + 2, e.CellBounds.Bottom - 4);
                        e.Graphics.DrawLine(p2, e.CellBounds.Left + 2, e.CellBounds.Bottom - 4, e.CellBounds.Left + 2, e.CellBounds.Top + 2);
                    }
                }
            }

            e.Handled = true;
        }
Beispiel #50
0
        /// <summary>
        /// 单元格绘制(重写)
        /// </summary>
        /// <param name="e"></param>
        /// <remarks></remarks>
        protected override void OnCellPainting(System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            try
            {
                if (e.RowIndex > -1 && e.ColumnIndex > -1)
                {
                    DrawCell(e);
                }
                else
                {
                    //行标题不重写
                    if (e.ColumnIndex < 0)
                    {
                        base.OnCellPainting(e);
                        return;
                    }

                    if (_columnDeep == 1)
                    {
                        base.OnCellPainting(e);
                        return;
                    }

                    //绘制表头
                    if (e.RowIndex == -1)
                    {
                        if (e.ColumnIndex >= NadirColumnList.Count)
                        {
                            e.Handled = true; return;
                        }
                        PaintUnitHeader((TreeNode)NadirColumnList[e.ColumnIndex]
                                        , e
                                        , _columnDeep);
                        e.Handled = true;
                    }
                }
            }
            catch
            { }
        }
Beispiel #51
0
        //绘制单元格
        private void dataGridView1_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            this.dgv_rota.ColumnHeadersDefaultCellStyle.BackColor = Color.Black;
            this.dgv_rota.Columns[1].Width = 300;//设置列宽度
            this.dgv_rota.Columns[2].Width = 300;
            this.dgv_rota.Columns[3].Width = 400;
            this.dgv_rota.Columns[4].Width = 650;
            //标题栏对语言对应
            this.dgv_rota.Columns[0].HeaderText = rota_str[0];
            this.dgv_rota.Columns[1].HeaderText = rota_str[1];
            this.dgv_rota.Columns[2].HeaderText = rota_str[2];
            this.dgv_rota.Columns[3].HeaderText = rota_str[3];
            this.dgv_rota.Columns[4].HeaderText = rota_str[4];

            this.dgv_rota.ColumnHeadersHeight         = 30;                                                     //设置标题栏高度
            this.dgv_rota.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing; //设置标题栏高度可调节
            e.CellStyle.Font      = new System.Drawing.Font("Segoe UI", 12F);
            e.CellStyle.BackColor = Color.FromArgb(45, 45, 48);
            e.CellStyle.ForeColor = Color.White;
            this.dgv_rota.EnableHeadersVisualStyles = false;
            this.dgv_rota.RowHeadersVisible         = false;
            foreach (string fieldHeaderText in colsHeaderText_H)
            {
                //纵向合并
                if (e.ColumnIndex >= 0 && this.dgv_rota.Columns[e.ColumnIndex].HeaderText == fieldHeaderText && e.RowIndex >= 0)
                {
                    using (
                        Brush gridBrush = new SolidBrush(this.dgv_rota.GridColor),
                        backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                    {
                        using (Pen gridLinePen = new Pen(gridBrush))
                        {
                            // 擦除原单元格背景
                            e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                            /****** 绘制单元格相互间隔的区分线条,datagridview自己会处理左侧和上边缘的线条,因此只需绘制下边框和和右边框
                             * DataGridView控件绘制单元格时,不绘制左边框和上边框,共用左单元格的右边框,上一单元格的下边框*****/

                            //不是最后一行且单元格的值不为null
                            if (e.RowIndex < this.dgv_rota.RowCount - 1 && this.dgv_rota.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value != null)
                            {
                                //手动设置第一列的值显示在第N行
                                if (e.ColumnIndex == 0)
                                {
                                    int position = SHIFT * list_type.Count % 2 == 0 ? SHIFT * list_type.Count / 2 - 1 : SHIFT * list_type.Count / 2;//用于定位日期显示位置
                                    if (e.RowIndex % (SHIFT * list_type.Count) == position)
                                    {
                                        e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
                                                              Convert.ToDateTime(e.Value.ToString()) <= DateTime.Now?Brushes.Gray:Brushes.Crimson, e.CellBounds.X + 2,
                                                              e.CellBounds.Y + 2, StringFormat.GenericDefault);
                                    }
                                }
                                //设置第二列
                                if (e.ColumnIndex == 1)
                                {
                                    int position = list_type.Count % 2 == 0 ? list_type.Count / 2 - 1 : list_type.Count / 2;//定位三班位置
                                    if (e.RowIndex % list_type.Count == position)
                                    {
                                        e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
                                                              Convert.ToDateTime(this.dgv_rota.Rows[e.RowIndex].Cells[0].Value.ToString()) <= DateTime.Now ? Brushes.Gray : Brushes.Crimson, e.CellBounds.X + 2,
                                                              e.CellBounds.Y + 2, StringFormat.GenericDefault);
                                    }
                                }
                                //若与下一单元格值不同
                                if (e.Value.ToString() != this.dgv_rota.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
                                {
                                    //下边缘的线
                                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                                                        e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                                    //绘制值
                                    if (e.Value != null)
                                    {
                                        if (e.ColumnIndex == 0 || e.ColumnIndex == 1)
                                        {
                                            e.Graphics.DrawString("", e.CellStyle.Font,
                                                                  Brushes.Crimson, e.CellBounds.X + 2,
                                                                  e.CellBounds.Y + 2, StringFormat.GenericDefault);
                                        }
                                        else
                                        {
                                            e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
                                                                  Brushes.Crimson, e.CellBounds.X + 2,
                                                                  e.CellBounds.Y + 2, StringFormat.GenericDefault);
                                        }
                                    }
                                }
                                //若与下一单元格值相同
                                else
                                {
                                    //背景颜色
                                    //e.CellStyle.BackColor = Color.LightPink;   //仅在CellFormatting方法中可用
                                    this.dgv_rota.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor     = Color.PowderBlue;
                                    this.dgv_rota.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Style.BackColor = Color.PowderBlue;
                                    //只读(以免双击单元格时显示值)
                                    this.dgv_rota.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly     = true;
                                    this.dgv_rota.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].ReadOnly = true;
                                    this.dgv_rota.Rows[e.RowIndex].Cells[2].ReadOnly = true;
                                    this.dgv_rota.Rows[e.RowIndex].Cells[3].ReadOnly = true;
                                }
                            }
                            //最后一行或单元格的值为null
                            else
                            {
                                //下边缘的线
                                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                                                    e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);

                                //绘制值
                                if (e.Value != null)
                                {
                                    if (e.ColumnIndex == 0 || e.ColumnIndex == 1)
                                    {
                                        e.Graphics.DrawString("", e.CellStyle.Font,
                                                              Brushes.Crimson, e.CellBounds.X + 2,
                                                              e.CellBounds.Y + 2, StringFormat.GenericDefault);
                                    }
                                    else
                                    {
                                        e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
                                                              Brushes.Crimson, e.CellBounds.X + 2,
                                                              e.CellBounds.Y + 2, StringFormat.GenericDefault);
                                    }
                                }
                            }
                            ////左侧的线()
                            //e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                            //    e.CellBounds.Top, e.CellBounds.Left,
                            //    e.CellBounds.Bottom - 1);

                            //右侧的线
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                                                e.CellBounds.Top, e.CellBounds.Right - 1,
                                                e.CellBounds.Bottom - 1);

                            //设置处理事件完成(关键点),只有设置为ture,才能显示出想要的结果。
                            e.Handled = true;
                        }
                    }
                }
            }
        }
Beispiel #52
0
        /// <summary>
        /// 画二维表头
        /// </summary>
        /// <param name="e"></param>
        private void DrawTableHead(System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            //二维表头
            if (e.RowIndex == -1)
            {
                if (SpanRows.ContainsKey(e.ColumnIndex)) //被合并的列
                {
                    //画边框
                    Graphics g = e.Graphics;
                    e.Paint(e.CellBounds, DataGridViewPaintParts.Background | DataGridViewPaintParts.Border);

                    int left = e.CellBounds.Left, top = e.CellBounds.Top + 2,
                        right = e.CellBounds.Right, bottom = e.CellBounds.Bottom;

                    switch (SpanRows[e.ColumnIndex].Position)
                    {
                    case 1:
                        left += 2;
                        break;

                    case 2:
                        break;

                    case 3:
                        right -= 2;
                        break;
                    }

                    //画上半部分底色
                    g.FillRectangle(new SolidBrush(this._mergecolumnheaderbackcolor), left, top,
                                    right - left, (bottom - top) / 2);

                    //画中线
                    g.DrawLine(new Pen(this.GridColor), left, (top + bottom) / 2,
                               right, (top + bottom) / 2);

                    //写小标题
                    StringFormat sf = new StringFormat();
                    sf.Alignment     = StringAlignment.Center;
                    sf.LineAlignment = StringAlignment.Center;

                    g.DrawString(e.Value + "", e.CellStyle.Font, Brushes.Black,
                                 new Rectangle(left, (top + bottom) / 2, right - left, (bottom - top) / 2), sf);
                    left = this.GetColumnDisplayRectangle(SpanRows[e.ColumnIndex].Left, true).Left - 2;

                    if (left < 0)
                    {
                        left = this.GetCellDisplayRectangle(-1, -1, true).Width;
                    }
                    right = this.GetColumnDisplayRectangle(SpanRows[e.ColumnIndex].Right, true).Right - 2;
                    if (right < 0)
                    {
                        right = this.Width;
                    }

                    g.DrawString(SpanRows[e.ColumnIndex].Text, e.CellStyle.Font, Brushes.Black,
                                 new Rectangle(left, top, right - left, (bottom - top) / 2), sf);
                    e.Handled = true;
                }
            }
        }
Beispiel #53
0
    /// <summary>
    /// 画字符串
    /// </summary>
    /// <param name="e"></param>
    /// <param name="cellwidth"></param>
    /// <param name="UpRows"></param>
    /// <param name="DownRows"></param>
    /// <param name="count"></param>
    private void PaintingFont(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, int cellTopHeight, int cellwidth, int cellheight, int UpRows, int DownRows, int count)
    {
        SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);
        //int fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
        //int fontwidth = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
        //int cellheight = e.CellBounds.Height;
        var rect = new RectangleF(e.CellBounds.X, e.CellBounds.Y - cellTopHeight, cellwidth, cellheight);

        using (var sf = new StringFormat())
        {
            switch (e.CellStyle.Alignment)
            {
            case DataGridViewContentAlignment.NotSet:
            case DataGridViewContentAlignment.TopLeft:
                sf.Alignment     = StringAlignment.Near;
                sf.LineAlignment = StringAlignment.Near;
                break;

            case DataGridViewContentAlignment.TopCenter:
                sf.Alignment     = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Near;
                break;

            case DataGridViewContentAlignment.TopRight:
                sf.Alignment     = StringAlignment.Far;
                sf.LineAlignment = StringAlignment.Near;
                break;

            case DataGridViewContentAlignment.MiddleLeft:
                sf.Alignment     = StringAlignment.Near;
                sf.LineAlignment = StringAlignment.Center;
                break;

            case DataGridViewContentAlignment.MiddleCenter:
                sf.Alignment     = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
                break;

            case DataGridViewContentAlignment.MiddleRight:
                sf.Alignment     = StringAlignment.Far;
                sf.LineAlignment = StringAlignment.Near;
                break;

            case DataGridViewContentAlignment.BottomLeft:
                sf.Alignment     = StringAlignment.Near;
                sf.LineAlignment = StringAlignment.Far;
                break;

            case DataGridViewContentAlignment.BottomCenter:
                sf.Alignment     = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Far;
                break;

            case DataGridViewContentAlignment.BottomRight:
                sf.Alignment     = StringAlignment.Far;
                sf.LineAlignment = StringAlignment.Far;
                break;

            default:
                break;
            }

            e.Graphics.DrawString(e.FormattedValue?.ToString(), e.CellStyle.Font, fontBrush, rect, sf);
        }


        //if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomCenter)
        //{
        //    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y + cellheight * DownRows - fontheight);
        //}
        //else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomLeft)
        //{
        //    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y + cellheight * DownRows - fontheight);
        //}
        //else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomRight)
        //{
        //    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y + cellheight * DownRows - fontheight);
        //}
        //else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleCenter)
        //{
        //    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
        //}
        //else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleLeft)
        //{
        //    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
        //}
        //else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleRight)
        //{
        //    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
        //}
        //else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopCenter)
        //{
        //    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1));
        //}
        //else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopLeft)
        //{
        //    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1));
        //}
        //else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopRight)
        //{
        //    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1));
        //}
        //else
        //{
        //    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
        //}
    }
Beispiel #54
0
        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
        }
Beispiel #55
0
        /// <summary>
        /// 重绘合并的单元格
        /// </summary>
        /// <param name="e"></param>
        private void PaintMergeCells(System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            if (this.MergeCellArray.Count == 0)
            {
                return;
            }
            else
            {
                Brush      gridBrush = new SolidBrush(this.GridColor);
                SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor);
                SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);
                //上面相同的行数
                int UpRows = 0;
                //下面相同的行数
                int DownRows = 0;
                //总行数
                int Rowcount   = 0;
                int cellwidth  = 0;
                int cellheight = 0;
                for (int j = 0; j < this.MergeCellArray.Count; j++)
                {
                    MergeCell mergeCell = (MergeCell)this.MergeCellArray[j];
                    if (e.RowIndex != -1 && e.RowIndex >= mergeCell.MergeMinRowIndex && e.RowIndex <= mergeCell.MergeMaxRowIndex && e.ColumnIndex >= mergeCell.MergeMinColumnIndex && e.ColumnIndex <= mergeCell.MergeMaxColumnIndex)
                    {
                        Pen gridLinePen = new Pen(gridBrush);
                        for (int i = mergeCell.MergeMinColumnIndex; i <= mergeCell.MergeMaxColumnIndex; i++)
                        {
                            cellwidth += this.Rows[e.RowIndex].Cells[i].Size.Width;
                        }
                        for (int i = mergeCell.MergeMinRowIndex; i <= mergeCell.MergeMaxRowIndex; i++)
                        {
                            cellheight += this.Rows[i].Cells[e.ColumnIndex].Size.Height;
                        }
                        DownRows = mergeCell.MergeMaxRowIndex - e.RowIndex + 1;
                        UpRows   = e.RowIndex - mergeCell.MergeMinRowIndex;
                        Rowcount = mergeCell.MergeMaxRowIndex - mergeCell.MergeMinRowIndex + 1;
                        if (this.Rows[e.RowIndex].Selected)
                        {
                            backBrush.Color = e.CellStyle.SelectionBackColor;
                            fontBrush.Color = e.CellStyle.SelectionForeColor;
                        }

                        // Determine the width before the current cell.
                        int nWidthLeft = 0;
                        int nHeightTop = 0;
                        for (int i = mergeCell.MergeMinColumnIndex; i < e.ColumnIndex; i++)
                        {
                            nWidthLeft += this.Rows[e.RowIndex].Cells[i].Size.Width;
                        }
                        for (int i = mergeCell.MergeMinRowIndex; i < e.RowIndex; i++)
                        {
                            nHeightTop += this.Rows[i].Cells[e.ColumnIndex].Size.Height;
                        }

                        RectangleF rectDest = new RectangleF(e.CellBounds.Left - nWidthLeft, e.CellBounds.Top - nHeightTop, cellwidth, cellheight);

                        //以背景色填充
                        e.Graphics.FillRectangle(backBrush, rectDest);

                        if (DownRows == 1)
                        {
                            e.Graphics.DrawLine(gridLinePen, rectDest.Left, e.CellBounds.Bottom - 1, rectDest.Right, e.CellBounds.Bottom - 1);
                            Rowcount = 0;
                        }
                        // 画右边线
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, rectDest.Top, e.CellBounds.Right - 1, rectDest.Bottom);


                        //画字符串
                        PaintingFont(e, this.Rows[mergeCell.MiddleCellRowIndex].Cells[mergeCell.MiddleCellColumnIndex].Value.ToString(), rectDest);
                        e.Handled = true;
                        break;
                    }
                }
            }
        }
        private void BorderDataGridView_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                TekenAchtergrond(e.Graphics, imgColumnHeader, e.CellBounds, 1);
                StringFormat format1;

                format1 = new StringFormat();
                format1.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;
                SizeF ef1 = e.Graphics.MeasureString((string)e.Value, this.Font, new SizeF((Single)e.CellBounds.Width, (Single)e.CellBounds.Height), format1);

                Size txts = System.Drawing.Size.Empty;

                txts = System.Drawing.Size.Ceiling(ef1);
                e.CellBounds.Inflate(-4, -4);

                Rectangle txtr = e.CellBounds;

                txtr = HAlignWithin(txts, txtr, ContentAlignment.MiddleCenter);
                txtr = VAlignWithin(txts, txtr, ContentAlignment.MiddleCenter);
                Brush brush2;

                format1 = new StringFormat();
                format1.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;

                brush2 = new SolidBrush(Color.FromArgb(39, 66, 62));

                e.Graphics.DrawString((string)e.Value, this.Font, brush2, (RectangleF)txtr, format1);
                brush2.Dispose();
                Pen       pen       = new Pen(Color.FromArgb(158, 182, 206));
                Rectangle recBorder = new Rectangle(e.CellBounds.X - 1, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height - 1);
                if (this.BorderStyle == BorderStyle.None)
                {
                    e.Graphics.DrawRectangle(pen, recBorder);
                }
                else
                {
                    e.Graphics.DrawLine(pen, e.CellBounds.X - 1, e.CellBounds.Y, e.CellBounds.X - 1, e.CellBounds.Y + e.CellBounds.Height - 1);
                    e.Graphics.DrawLine(pen, e.CellBounds.X - 1, e.CellBounds.Y + e.CellBounds.Height - 1, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Y + e.CellBounds.Height - 1);
                    e.Graphics.DrawLine(pen, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Y, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Y + e.CellBounds.Height - 1);
                }

                if (SortedColumnIndex >= 0 && e.ColumnIndex == SortedColumnIndex)
                {
                    Image image = null;
                    if (this.SortOrder == SortOrder.Ascending)
                    {
                        image = imgSortAscending;
                    }
                    if (this.SortOrder == SortOrder.Descending)
                    {
                        image = imgSortDescending;
                    }

                    if (image != null)
                    {
                        int x = 0;
                        int y = 0;
                        if (e.CellBounds.Width > 16)
                        {
                            x = e.CellBounds.X + e.CellBounds.Width - 16;
                        }
                        else
                        {
                            x = e.CellBounds.X;
                        }
                        if (e.CellBounds.Height > image.Height)
                        {
                            y = e.CellBounds.Y + (e.CellBounds.Height - image.Height) / 2;
                        }
                        else
                        {
                            y = e.CellBounds.Y;
                        }

                        e.Graphics.DrawImage(image, x, y, image.Width, image.Height);
                    }
                }
                e.Handled = true;
            }

            if (e.ColumnIndex == -1)
            {
                TekenAchtergrond(e.Graphics, imgColumnHeader, e.CellBounds, 1);

                Pen       pen       = new Pen(Color.FromArgb(158, 182, 206));
                Rectangle recBorder = new Rectangle(e.CellBounds.X - 1, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height - 1);
                if (this.BorderStyle == BorderStyle.None)
                {
                    e.Graphics.DrawRectangle(pen, recBorder);
                }
                else
                {
                    e.Graphics.DrawLine(pen, e.CellBounds.X - 1, e.CellBounds.Y, e.CellBounds.X - 1, e.CellBounds.Y + e.CellBounds.Height - 1);
                    e.Graphics.DrawLine(pen, e.CellBounds.X - 1, e.CellBounds.Y + e.CellBounds.Height - 1, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Y + e.CellBounds.Height - 1);
                    e.Graphics.DrawLine(pen, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Y, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Y + e.CellBounds.Height - 1);
                }

                if (e.RowIndex >= 0)
                {
                    Image image = null;
                    if (this.Rows[e.RowIndex].IsNewRow)
                    {
                        image = imgNewRow;
                    }
                    if (this.CurrentCell != null)
                    {
                        if (this.CurrentCell.RowIndex == e.RowIndex && this.Rows[e.RowIndex].IsNewRow)
                        {
                            image = imgCurrentNew;
                        }
                        else if (this.CurrentCell.RowIndex == e.RowIndex && this.CurrentCell.IsInEditMode)
                        {
                            image = imgEdit;
                        }
                        else if (this.CurrentCell.RowIndex == e.RowIndex)
                        {
                            image = imgCurrent;
                        }
                    }

                    if (image != null)
                    {
                        int x = 0;
                        int y = 0;
                        if (e.CellBounds.Width > 16)
                        {
                            x = e.CellBounds.X + e.CellBounds.Width - 16;
                        }
                        else
                        {
                            x = e.CellBounds.X;
                        }
                        if (e.CellBounds.Height > image.Height)
                        {
                            y = e.CellBounds.Y + (e.CellBounds.Height - image.Height) / 2;
                        }
                        else
                        {
                            y = e.CellBounds.Y;
                        }

                        e.Graphics.DrawImage(image, x, y, image.Width, image.Height);
                    }
                }
                e.Handled = true;
            }
        }
        ///<summary>
        ///绘制合并表头--绘制表头思路改进
        ///</summary>
        ///<param name="node">合并表头节点</param>
        ///<param name="e">绘图参数集</param>
        ///<param name="level">结点深度</param>
        ///<remarks></remarks>
        private void PaintUnitHeader(DataGridViewColumnNode node, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            bool columnVisible = this.Columns[e.ColumnIndex].Visible;

            Rectangle  uhRectangle;
            SolidBrush backColorBrush = new SolidBrush(e.CellStyle.BackColor);

            int fakeLevel = node.FakeLevel;

            if (node.ChildColumns.Count == 0)
            {
                if (e.ColumnIndex == 1)
                {
                }
                if (e.ColumnIndex == 2)
                {
                }

                #region 叶子

                //此列没有子列,且是顶级列
                if (fakeLevel == 0)
                {
                    uhRectangle = new Rectangle(e.CellBounds.Left - 1,
                                                e.CellBounds.Top + fakeLevel * realHeadCellHeight - 1,
                                                e.CellBounds.Width,
                                                this.ColumnHeadersHeight);
                }
                //此列是最后一列
                else if (fakeLevel == columnDeep - 1)
                {
                    uhRectangle = new Rectangle(e.CellBounds.Left - 1,
                                                e.CellBounds.Top + fakeLevel * realHeadCellHeight - 1,
                                                e.CellBounds.Width,
                                                this.ColumnHeadersHeight - realHeadCellHeight * (columnDeep - 1));
                }
                //其它情况
                else
                {
                    uhRectangle = new Rectangle(e.CellBounds.Left - 1,
                                                e.CellBounds.Top + fakeLevel * realHeadCellHeight - 1,
                                                e.CellBounds.Width,
                                                realHeadCellHeight * (columnDeep - fakeLevel));
                }

                if (e.ColumnIndex == 2)
                {
                }

                DrawCell(node, e, uhRectangle);

                #endregion
            }
            else
            {
                #region 非叶子

                //取得最左叶子节点
                DataGridViewColumnNode newtreenode1 = node;
                while (newtreenode1.FristColumn != null)
                {
                    newtreenode1 = newtreenode1.FristColumn;
                }
                //取得最左叶子节点列号
                int leftindex = GetColumnListNodeIndex(newtreenode1);

                //计算要减去的距离
                int leftrangge = 0;

                for (int i = leftindex; i < e.ColumnIndex; i++)
                {
                    leftrangge += this.Columns[i].Width;
                }

                //int visibleColumnsCount = GetVisibleColumnsCount();
                //得到给节点对应的宽度
                int nodewidth = GetUnitHeaderWidth(node);
                //得到矩形区域
                uhRectangle = new Rectangle(e.CellBounds.Left - leftrangge - 1,
                                            e.CellBounds.Top + fakeLevel * realHeadCellHeight,
                                            nodewidth,
                                            realHeadCellHeight);

                DrawCell(node, e, uhRectangle);

                #endregion
            }

            #region 递归

            //递归调用 使得每列触发该列上所有节点的重绘
            if (node.Parent != null)
            {
                PaintUnitHeader(node.Parent, e);
            }

            #endregion
        }
Beispiel #58
0
        //绘制单元格
        private void dataGridView_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            foreach (string fieldHeaderText in colsHeaderText_H)
            {
                //纵向合并
                if (e.ColumnIndex >= 0 && this.dataGridView.Columns[e.ColumnIndex].HeaderText == fieldHeaderText && e.RowIndex >= 0)
                {
                    using (
                        Brush gridBrush = new SolidBrush(this.dataGridView.GridColor),
                        backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                    {
                        using (Pen gridLinePen = new Pen(gridBrush))
                        {
                            // 擦除原单元格背景
                            e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                            /****** 绘制单元格相互间隔的区分线条,datagridview自己会处理左侧和上边缘的线条,因此只需绘制下边框和和右边框
                             * DataGridView控件绘制单元格时,不绘制左边框和上边框,共用左单元格的右边框,上一单元格的下边框*****/

                            //不是最后一行且单元格的值不为null
                            if (e.RowIndex < this.dataGridView.RowCount - 1 && this.dataGridView.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value != null)
                            {
                                //若与下一单元格值不同
                                if (e.Value.ToString() != this.dataGridView.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
                                {
                                    //下边缘的线
                                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                                                        e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                                    //绘制值
                                    if (e.Value != null)
                                    {
                                        e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
                                                              Brushes.Crimson, e.CellBounds.X + 2,
                                                              e.CellBounds.Y + 2, StringFormat.GenericDefault);
                                    }
                                }
                                //若与下一单元格值相同
                                else
                                {
                                    //背景颜色
                                    //e.CellStyle.BackColor = Color.LightPink;   //仅在CellFormatting方法中可用
                                    this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor     = Color.LightBlue;
                                    this.dataGridView.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Style.BackColor = Color.LightBlue;
                                    //只读(以免双击单元格时显示值)
                                    this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly     = true;
                                    this.dataGridView.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].ReadOnly = true;
                                }
                            }
                            //最后一行或单元格的值为null
                            else
                            {
                                //下边缘的线
                                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                                                    e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);

                                //绘制值
                                if (e.Value != null)
                                {
                                    e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
                                                          Brushes.Crimson, e.CellBounds.X + 2,
                                                          e.CellBounds.Y + 2, StringFormat.GenericDefault);
                                }
                            }

                            ////左侧的线()
                            //e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                            //    e.CellBounds.Top, e.CellBounds.Left,
                            //    e.CellBounds.Bottom - 1);

                            //右侧的线
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                                                e.CellBounds.Top, e.CellBounds.Right - 1,
                                                e.CellBounds.Bottom - 1);

                            //设置处理事件完成(关键点),只有设置为ture,才能显示出想要的结果。
                            e.Handled = true;
                        }
                    }
                }
            }

            foreach (string fieldHeaderText in colsHeaderText_V)
            {
                //横向合并
                if (e.ColumnIndex >= 0 && this.dataGridView.Columns[e.ColumnIndex].HeaderText == fieldHeaderText && e.RowIndex >= 0)
                {
                    using (
                        Brush gridBrush = new SolidBrush(this.dataGridView.GridColor),
                        backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                    {
                        using (Pen gridLinePen = new Pen(gridBrush))
                        {
                            // 擦除原单元格背景
                            e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                            /****** 绘制单元格相互间隔的区分线条,datagridview自己会处理左侧和上边缘的线条,因此只需绘制下边框和和右边框
                             * DataGridView控件绘制单元格时,不绘制左边框和上边框,共用左单元格的右边框,上一单元格的下边框*****/

                            //不是最后一列且单元格的值不为null
                            if (e.ColumnIndex < this.dataGridView.ColumnCount - 1 && this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value != null)
                            {
                                if (e.Value.ToString() != this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())
                                {
                                    //右侧的线
                                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,
                                                        e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                                    //绘制值
                                    if (e.Value != null)
                                    {
                                        e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
                                                              Brushes.Crimson, e.CellBounds.X + 2,
                                                              e.CellBounds.Y + 2, StringFormat.GenericDefault);
                                    }
                                }
                                //若与下一单元格值相同
                                else
                                {
                                    //背景颜色
                                    //e.CellStyle.BackColor = Color.LightPink;   //仅在CellFormatting方法中可用
                                    this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor     = Color.LightPink;
                                    this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Style.BackColor = Color.LightPink;
                                    //只读(以免双击单元格时显示值)
                                    this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly     = true;
                                    this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].ReadOnly = true;
                                }
                            }
                            else
                            {
                                //右侧的线
                                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,
                                                    e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);

                                //绘制值
                                if (e.Value != null)
                                {
                                    e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
                                                          Brushes.Crimson, e.CellBounds.X + 2,
                                                          e.CellBounds.Y + 2, StringFormat.GenericDefault);
                                }
                            }
                            //下边缘的线
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                                                e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                            e.Handled = true;
                        }
                    }
                }
            }
        }
Beispiel #59
0
        /// <summary>
        /// 重绘合并的列
        /// </summary>
        /// <param name="e"></param>
        private void PaintMergeColumns(System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            Brush      gridBrush = new SolidBrush(this.GridColor);
            SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor);
            SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);
            int        cellwidth;
            //上面相同的行数
            int UpRows = 0;
            //下面相同的行数
            int DownRows = 0;
            //总行数
            int Rowcount = 0;

            cellwidth = e.CellBounds.Width;
            Pen    gridLinePen = new Pen(gridBrush);
            string curValue    = e.Value == null ? "" : e.Value.ToString().Trim();
            string curSelected = this.CurrentRow.Cells[e.ColumnIndex].Value == null ? "" : this.CurrentRow.Cells[e.ColumnIndex].Value.ToString().Trim();

            if (!string.IsNullOrEmpty(curValue))
            {
                #region 获取下面的行数
                for (int i = e.RowIndex; i < this.Rows.Count; i++)
                {
                    if (this.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue))
                    {
                        //this.Rows[i].Cells[e.ColumnIndex].Selected = this.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected;

                        DownRows++;
                        if (e.RowIndex != i)
                        {
                            cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                #endregion
                #region 获取上面的行数
                for (int i = e.RowIndex; i >= 0; i--)
                {
                    if (this.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue))
                    {
                        //this.Rows[i].Cells[e.ColumnIndex].Selected = this.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected;
                        UpRows++;
                        if (e.RowIndex != i)
                        {
                            cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                #endregion
                Rowcount = DownRows + UpRows - 1;
                if (Rowcount < 2)
                {
                    return;
                }
            }
            if (this.Rows[e.RowIndex].Selected)
            {
                backBrush.Color = e.CellStyle.SelectionBackColor;
                fontBrush.Color = e.CellStyle.SelectionForeColor;
            }
            //以背景色填充
            e.Graphics.FillRectangle(backBrush, e.CellBounds);
            //画字符串
            PaintingFont(e, cellwidth, UpRows, DownRows, Rowcount);
            if (DownRows == 1)
            {
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                Rowcount = 0;
            }
            // 画右边线
            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);

            e.Handled = true;
        }