如何在C#winforms中设置自定义DataGridView边框

ROM*_*ROM 2 .net c# datagridview winforms

我被困在这里,如果任何身体可以帮助我想打印datagridview标题行与下面的单个虚线只是像:

Name              ID
---------------------
Run Code Online (Sandbox Code Playgroud)

并打印没有这样的任何边框的项目

Name              ID
---------------------
Abc               21
Run Code Online (Sandbox Code Playgroud)

我用过这段代码

dgvmain.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
dgvmain.CellBorderStyle = DataGridViewCellBorderStyle.None;
Run Code Online (Sandbox Code Playgroud)

dgvmain我的DataGridView Any 名称在哪里提前感谢.

Kin*_*ing 6

您需要通过向CellPainting事件处理程序添加代码来进行一些自定义绘制.要设置单元格边框None,请使用CellBorderStyle:

dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;

// CellPainting event handler for your dataGridView1
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex == -1 && e.ColumnIndex > -1)
    {
       e.Handled = true;
       using (Brush b = new SolidBrush(dataGridView1.DefaultCellStyle.BackColor))
       {
         e.Graphics.FillRectangle(b, e.CellBounds);
       }
       using (Pen p = new Pen(Brushes.Black))
       {
         p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
         e.Graphics.DrawLine(p, new Point(0, e.CellBounds.Bottom-1), new Point(e.CellBounds.Right, e.CellBounds.Bottom-1));
       }
       e.PaintContent(e.ClipBounds);
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述