Exemple #1
0
        private void dgvData_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            int rowIndex = e.RowIndex;
            int colIndex = e.ColumnIndex;

            if (rowIndex >= 0)
            {
                if (11 == colIndex)
                {
                    string         Id   = this.dgvData.Rows[rowIndex].Cells["Id"].Value as string;
                    EdGroupAddress addr = MyCache.GetGroupAddress(Id);
                    if ((null != addr) && (null != addr.Actions))
                    {
                        //画单元格的边界线
                        Point   p1 = new Point(e.CellBounds.Left + e.CellBounds.Width, e.CellBounds.Top);
                        Point   p2 = new Point(e.CellBounds.Left + e.CellBounds.Width, e.CellBounds.Top + e.CellBounds.Height);
                        Point   p3 = new Point(e.CellBounds.Left, e.CellBounds.Top + e.CellBounds.Height);
                        Point[] ps = new Point[] { p1, p2, p3 };
                        using (Brush gridBrush = new SolidBrush(this.dgvData.GridColor))
                        {
                            using (Pen gridLinePen = new Pen(gridBrush, 2))
                            {
                                Font   font          = new Font("宋体", 9, FontStyle.Regular);//自定义字体
                                string seperator     = ";";
                                SizeF  sizeSeperator = e.Graphics.MeasureString(seperator, font);
                                float  startPos      = .0f;

                                //判断当前行是否为选中行,如果为选中行,则要修改图片的背景色和文字的字体颜色
                                if (this.dgvData.CurrentRow.Index == e.RowIndex)
                                {
                                    using (Brush backColorBrush = new SolidBrush(Color.FromArgb(051, 153, 255)))
                                    {
                                        //以背景色填充单元格
                                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                                        e.Graphics.DrawLines(gridLinePen, ps);

                                        foreach (DatapointActionNode action in addr.Actions)
                                        {
                                            if (startPos >= e.CellBounds.Width)
                                            {
                                                break;
                                            }

                                            if (startPos > 0)
                                            {
                                                e.Graphics.DrawString(seperator, font, Brushes.White, new RectangleF(e.CellBounds.Left + startPos,
                                                                                                                     e.CellBounds.Top + (e.CellBounds.Height - sizeSeperator.Height) / 2, e.CellBounds.Width - startPos,
                                                                                                                     sizeSeperator.Height), StringFormat.GenericDefault);
                                                startPos += sizeSeperator.Width;
                                            }

                                            SizeF sizeText = e.Graphics.MeasureString(action.Name, font);
                                            e.Graphics.DrawString(action.Name, font, Brushes.White, new RectangleF(e.CellBounds.Left + startPos,
                                                                                                                   e.CellBounds.Top + (e.CellBounds.Height - sizeText.Height) / 2, e.CellBounds.Width - startPos,
                                                                                                                   sizeText.Height), StringFormat.GenericDefault);
                                            startPos += sizeText.Width;
                                        }

                                        e.Handled = true;
                                    }
                                }
                                else
                                {
                                    using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                                    {
                                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                                        e.Graphics.DrawLines(gridLinePen, ps);

                                        foreach (DatapointActionNode action in addr.Actions)
                                        {
                                            if (startPos >= e.CellBounds.Width)
                                            {
                                                break;
                                            }

                                            if (startPos > 0)
                                            {
                                                e.Graphics.DrawString(seperator, font, Brushes.Black, new RectangleF(e.CellBounds.Left + startPos,
                                                                                                                     e.CellBounds.Top + (e.CellBounds.Height - sizeSeperator.Height) / 2, e.CellBounds.Width - startPos,
                                                                                                                     sizeSeperator.Height), StringFormat.GenericDefault);
                                                startPos += sizeSeperator.Width;
                                            }

                                            if (startPos >= e.CellBounds.Width)
                                            {
                                                break;
                                            }

                                            SizeF sizeText = e.Graphics.MeasureString(action.Name, e.CellStyle.Font);
                                            e.Graphics.DrawString(action.Name, e.CellStyle.Font, Brushes.Blue, new RectangleF(e.CellBounds.Left + startPos,
                                                                                                                              e.CellBounds.Top + (e.CellBounds.Height - sizeText.Height) / 2, e.CellBounds.Width - startPos,
                                                                                                                              sizeText.Height), StringFormat.GenericDefault);
                                            startPos += sizeText.Width;
                                        }

                                        e.Handled = true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }