2

如果有任何机构可以帮助我想用下面的单条虚线打印 datagridview 标题行,我就会陷入困境,例如:

Name              ID
---------------------

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

Name              ID
---------------------
Abc               21

我用了这段代码

dgvmain.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
dgvmain.CellBorderStyle = DataGridViewCellBorderStyle.None;

dgvmain我的任何帮助的名字在哪里DataGridView 提前谢谢。

4

1 回答 1

7

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);
    }
}

在此处输入图像描述

于 2013-08-18T08:58:48.227 回答