在DataGridView中隐藏特定行中的网格线

本文关键字:网格线 DataGridView 隐藏 | 更新日期: 2023-09-27 17:54:22

我想要得到一个空白行。我想在特定的行中隐藏网格线。我该怎么做呢?

Grid.CellBorderStyle = DataGridViewCellBorderStyle.None;

但这可以应用到网格

在DataGridView中隐藏特定行中的网格线

未经测试,但是您应该能够通过处理CellPainting事件并排除datagridviewpart来获得所需的内容。边境

  e.Paint(e.ClipBounds, DataGridViewPaintParts.All ^ DataGridViewPaintParts.Border);
  e.Handled = true;

这不是完美的,但我希望你能得到一些想法。

DataGridView设计

this.dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
this.dataGridView1.AllowUserToResizeRows = false;
this.dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;

 private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
    if (e.RowIndex == -1) return;
    // Calculate the bounds of the row.
    Rectangle rowBounds = new Rectangle(
        this.dataGridView1.RowHeadersWidth, e.RowBounds.Top,
        this.dataGridView1.Columns.GetColumnsWidth(
            DataGridViewElementStates.Visible) -
        this.dataGridView1.HorizontalScrollingOffset + 1,
        e.RowBounds.Height);
    // Paint the custom background. 
    using (Brush backbrush =
       new SolidBrush(this.dataGridView1.GridColor), backColorBrush = new SolidBrush(Color.White))
    {
        using (Pen gridLinePen = new Pen(backbrush))
        {
            //Apply to spicific row
            if (e.RowIndex == 2)
            {
                e.Graphics.FillRectangle(backbrush, rowBounds);
                // Draw the inset highlight box.
                e.Graphics.DrawRectangle(Pens.Blue, rowBounds);
            }
        }
    }
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex == -1) return;
    if (e.Value != null)
    {
        // Draw the text content of the cell, ignoring alignment of e.RowIndex != 2
        if (e.RowIndex != 2)
        {
            e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
           Brushes.Black, e.CellBounds.X + 2,
           e.CellBounds.Y + 2, StringFormat.GenericDefault);
        }
    }
    e.Handled = true;
}

引用:
DataGridView。RowPrePaint事件
DataGridView。CellPainting事件