如何在编辑DataGridView单元格时绘制边框?

时间:2015-08-22 10:05:21

标签: c# .net winforms datagridview

我想在DataGridView单元格周围绘制一个红色边框,然后进行编辑。

我设法在所选单元格周围绘制一个红色边框,但未使用此代码进行编辑:

private void Form1_Load(object sender, EventArgs e)
{
    this.Width = 650;
    this.Height = 250;
    dataGridView1.Left = 5;
    dataGridView1.Top = 5;
    dataGridView1.Width = 600;
    dataGridView1.Height = 175;

    DataTable dt = new DataTable("Test Table");
    dt.Columns.Add("Column 1");
    dt.Columns.Add("Column 2");
    dt.Columns.Add("Column 3");
    dt.Columns.Add("Column 4");
    dt.Columns.Add("Column 5");
    dt.Rows.Add(dt.NewRow());
    dt.Rows.Add(dt.NewRow());
    dt.Rows.Add(dt.NewRow());
    dt.Rows.Add(dt.NewRow());
    dt.Rows.Add(dt.NewRow());
    dataGridView1.DataSource = dt;

    dataGridView1.AllowUserToAddRows = false;
    dataGridView1.AllowUserToDeleteRows = false;
    dataGridView1.MultiSelect = false;
    dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
    dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White;
    dataGridView1.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(this.dataGridView1_CellPainting);
    dataGridView1.EditingControlShowing += new System.Windows.Forms.DataGridViewEditingControlShowingEventHandler(this.dataGridView1_EditingControlShowing);
}

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.ColumnIndex != -1 && e.RowIndex != -1 && dataGridView1[e.ColumnIndex, e.RowIndex].Selected)
    {
        using (Brush borderBrush = new SolidBrush(Color.Red))
        {
            using (Pen borderPen = new Pen(borderBrush, 2))
            {
                Rectangle rectDimensions = e.CellBounds;
                rectDimensions.Width -= 2;
                rectDimensions.Height -= 2;
                rectDimensions.X = rectDimensions.Left + 1;
                rectDimensions.Y = rectDimensions.Top + 1;

                e.Graphics.DrawRectangle(borderPen, rectDimensions);

                e.Handled = true;
            }
        }
    }
}

产生这个结果:

Image1

但是,当您编辑单元格时会发生这种情况:

Image2

似乎EditingControl正在我的大部分红色边框上方绘制。很遗憾,我无法找到解决此问题的方法,因此我的红色边框始终会完全显示。

我该怎么办?


这是我迄今为止所尝试的内容:

1。处理EditingControlShowing()事件以手动重新绘制     像这样的边界:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    Graphics gfx = e.Control.CreateGraphics();

    using (Brush borderBrush = new SolidBrush(Color.Red))
    {
        using (Pen borderPen = new Pen(borderBrush, 2))
        {
            Rectangle rectDimensions = e.Control.ClientRectangle;
            rectDimensions.Width -= 2;
            rectDimensions.Height -= 2;
            rectDimensions.X = rectDimensions.Left + 1;
            rectDimensions.Y = rectDimensions.Top + 1;

            gfx.DrawRectangle(borderPen, rectDimensions);
        }
    }
}

但这并没有吸引任何东西。我尝试了一些这方面的变化,但所有这些仍然没有在这里画。


2。然后我尝试处理Paint()的{​​{1}}事件     像这样:

EditingControl

但是这个事件甚至没有发生。我后来读到了那个地方     private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { e.Control.Paint -= new PaintEventHandler(dataGridView1_EditingControl_Paint); e.Control.Paint += new PaintEventHandler(dataGridView1_EditingControl_Paint); } void dataGridView1_EditingControl_Paint(object sender, PaintEventArgs e) { MessageBox.Show("Starting EditingControl Paint() Event..."); } 使用正常的EditingControl,但不会触发。{1}}     TextBox事件,因为它由Windows处理。


3。最后,我没有尝试重新绘制另一个边框,而是决定     尝试通过调整Paint()的大小来破解它     比我的边界小,希望边界会显示出来     它,像这样:

EditingControl

然而,这只是给了我这个结果:

Image3

所以private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { e.Control.Resize -= new EventHandler(dataGridView1_EditingControl_Resize); e.Control.Resize += new EventHandler(dataGridView1_EditingControl_Resize); } void dataGridView1_EditingControl_Resize(object sender, EventArgs e) { dataGridView1.EditingControl.Left = 20; } 确实向左移动了,但似乎还有另一个     控制在它下面仍然阻挡我的红色边框。但是,无论如何我无法获得访问权限     为了控制它的大小,所以这对我来说也没有用。


4. 我还尝试使用上面#1中的代码重新绘制TextBox事件中的边框,但这仍然没有做任何事情。虽然,使用Resize()确实有效,所以我可以在这里格式化控件的某些部分,但似乎试图绘制边框不是其中之一。

我想做的就是在正在编辑的单元格周围显示一个红色边框。你知道我怎么做吗?

2 个答案:

答案 0 :(得分:8)

可以使用一些设置和绘制单元格的特定部分来完成。如此:

首先,在设计器中将CellBorderStyle设置为RaisedSunken,或者只是在表单加载代码中设置:

this.dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Raised;

然后使用这些特定的有序规则绘制单元格:

  1. 仅绘制网格内容单元格而不绘制ColumnHeader单元格或RowHeader
  2. 绘制选定的单元格时,首先使用e.Paint(...)绘制除边框以外的所有部分;然后自己画边框
  3. 设置e.Handled = true以防止默认绘画
  4. 绘制未选定的单元格时,首先使用e.Paint(...)
  5. 绘制除边框以外的所有部分
  6. 使用网格背景颜色绘制第一列单元格的第一行和左边框的单元格的顶部边框
  7. 使用网格线颜色
  8. 绘制最后一行单元格和最后一列单元格右边框的底边框
  9. 使用网格背景颜色绘制非最后一行单元格的右边框和非最后一列单元格右边框的底边框
  10. 使用网格线颜色绘制非最后一列的单元格的非第一行和左边框的顶部边框单元格 9.设置e.Handled = true以防止默认绘画
  11. 这是选择

    后的结果屏幕截图

    enter image description here

    这里是编辑单元格时结果的截图

    enter image description here

    这是细胞绘制事件的代码:

    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        //Draw only grid content cells not ColumnHeader cells nor RowHeader cells
        if (e.ColumnIndex > -1 & e.RowIndex > -1)
        {
            //Pen for left and top borders
            using (var backGroundPen = new Pen(e.CellStyle.BackColor, 1))
            //Pen for bottom and right borders
            using (var gridlinePen = new Pen(dataGridView1.GridColor, 1))
            //Pen for selected cell borders
            using (var selectedPen = new Pen(Color.Red, 1))
            {
                var topLeftPoint = new Point(e.CellBounds.Left, e.CellBounds.Top);
                var topRightPoint = new Point(e.CellBounds.Right - 1, e.CellBounds.Top);
                var bottomRightPoint = new Point(e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                var bottomleftPoint = new Point(e.CellBounds.Left, e.CellBounds.Bottom - 1);
    
                //Draw selected cells here
                if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Selected)
                {
                    //Paint all parts except borders.
                    e.Paint(e.ClipBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border);
    
                    //Draw selected cells border here
                    e.Graphics.DrawRectangle(selectedPen, new Rectangle(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width - 1, e.CellBounds.Height - 1));
    
                    //Handled painting for this cell, Stop default rendering.
                    e.Handled = true;
                }
                //Draw non-selected cells here
                else
                {
                    //Paint all parts except borders.
                    e.Paint(e.ClipBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border);
    
                    //Top border of first row cells should be in background color
                    if (e.RowIndex == 0)
                        e.Graphics.DrawLine(backGroundPen, topLeftPoint, topRightPoint);
    
                    //Left border of first column cells should be in background color
                    if (e.ColumnIndex == 0)
                        e.Graphics.DrawLine(backGroundPen, topLeftPoint, bottomleftPoint);
    
                    //Bottom border of last row cells should be in gridLine color
                    if (e.RowIndex == dataGridView1.RowCount - 1)
                        e.Graphics.DrawLine(gridlinePen, bottomRightPoint, bottomleftPoint);
                    else  //Bottom border of non-last row cells should be in background color
                        e.Graphics.DrawLine(backGroundPen, bottomRightPoint, bottomleftPoint);
    
                    //Right border of last column cells should be in gridLine color
                    if (e.ColumnIndex == dataGridView1.ColumnCount - 1)
                        e.Graphics.DrawLine(gridlinePen, bottomRightPoint, topRightPoint);
                    else //Right border of non-last column cells should be in background color
                        e.Graphics.DrawLine(backGroundPen, bottomRightPoint, topRightPoint);
    
                    //Top border of non-first row cells should be in gridLine color, and they should be drawn here after right border
                    if (e.RowIndex > 0)
                        e.Graphics.DrawLine(gridlinePen, topLeftPoint, topRightPoint);
    
                    //Left border of non-first column cells should be in gridLine color, and they should be drawn here after bottom border
                    if (e.ColumnIndex > 0)
                        e.Graphics.DrawLine(gridlinePen, topLeftPoint, bottomleftPoint);
    
                    //We handled painting for this cell, Stop default rendering.
                    e.Handled = true;
                }
            }
        }
    }
    

答案 1 :(得分:2)

使用现有代码的最简单方法是将CellBorderStyle设置为沉没,如下所示:

dataGridView1.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.Sunken;

enter image description here

如果您不喜欢沉没,那么您可以通过AdjustCellBorderStyleDataGridViewAdvancedBorderStyle实现此目的,在单元格焦点事件更改/自定义单元格边框样式。 另请查看:How to: Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance

我希望它能为你提供帮助。