在datagridview中应用cellpainting后,选定的单元格不再突出显示

时间:2016-06-23 15:52:50

标签: c# winforms datagridview

我是StackOverFlow的新手,这是我的第一个问题,所以如果问题没有根据Stack Overflow标准制定,请不要对新手很难。

我在CellPainting上调用DataGridView事件以格式化一些单元格文本(文本包含HTML标记,我正在更改这些标记的字体)。我设法应用格式并在datagridview中显示它,但调用cellpainting事件会改变datagridview的行为,如下所示:

  1. 当我选择绘制的单元格(我只绘制包含标签的单元格)时,所选单元格不会以蓝色突出显示。看起来好像没有应用选择背景颜色,即使我在代码中定义了它。
  2. 文本延伸(字符之间的空格增加)。我已经尝试改变排版,但它没有帮助。
  3. 有谁知道如何让选择出现以及如何避免文字拉伸?我会感激任何可以提供帮助的人。

    我在这个问题的底部复制了cellpainting事件的代码。

    提前感谢您的支持。

    此致

    劳伦

    PS:我正在从sqlite数据库加载数据。

    //Creating the table of string where the formatting should change 
    Debug.Print(e.Value.ToString());
    string[] cellFormattingStr = _tagRegex.Split(e.Value.ToString());
    Debug.Print(String.Join(Environment.NewLine, cellFormattingStr));
    
    //Get cell formatting as it is
    DataGridView currentDGV = (DataGridView)sender;
    
    Rectangle newRect = new Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1, e.CellBounds.Width - 4, e.CellBounds.Height - 4);
    //Rectangle newRect = new Rectangle(e.CellBounds.Location, e.CellBounds.Size);
    
    using (Brush backColor = new SolidBrush(e.CellStyle.BackColor), gridBrush = new SolidBrush(currentDGV.GridColor))
    {
        using (Pen gridLinePen = new Pen(gridBrush))
        {
            //Erase the cell
            e.Graphics.FillRectangle(backColor, e.CellBounds);
    
            //Draw the gridlines
            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);
    
            e.CellStyle.SelectionBackColor = Color.Blue;
    
            //Draw the inset highlight box.
            e.Graphics.DrawRectangle(Pens.Blue, newRect);
    
            int cellBoundX = e.CellBounds.X + 2;
            int cellBoundY = e.CellBounds.Y + 2;
            int lineLengthAfterIns = 2;
    
            for (int cfs = 0; cfs < cellFormattingStr.Length; cfs++)
            {
                Brush txtColor = null;
                if (cellFormattingStr[cfs].StartsWith("<"))
                    txtColor = Brushes.Red;
                else
                    txtColor = Brushes.Black;
    
                //Creating the table of words for the part to be formatted
                string[] fmtdWords = cellFormattingStr[cfs].Split(new string[]{" "}, StringSplitOptions.RemoveEmptyEntries);
    
                for (int w = 0; w < fmtdWords.Length; w++)
                {
    
                    //Check the size of words and add it to the xbound to see if it exceeds size of cell. If yes, xbound is reset to initial value and ybound increased to the size of a new line
                    SizeF wdDim = e.Graphics.MeasureString(fmtdWords[w], e.CellStyle.Font);
    
                    lineLengthAfterIns += (int)wdDim.Width;
                    Debug.Print(fmtdWords[w] + "; " + lineLengthAfterIns + "; " + e.CellBounds.Width);
                    if (lineLengthAfterIns >= e.CellBounds.Width - 4)
                    {
                        cellBoundX = e.CellBounds.X + 2;
                        cellBoundY += (int)wdDim.Height + 2;
                        lineLengthAfterIns = 2;
                    }
    
                    e.Graphics.DrawString(fmtdWords[w], e.CellStyle.Font, txtColor, cellBoundX, cellBoundY, StringFormat.GenericTypographic);
                    cellBoundX += (int)wdDim.Width;
                }
    
            }
            e.Handled = true;
        }
    }
    

1 个答案:

答案 0 :(得分:0)

在绘制文本之前,使用: e.PaintBackground(newRect,true);