如果任何机构可以帮助我想用下面的单虚线打印 datagridview 标题行,我就会陷入困境,例如:
Name ID
---------------------
并像这样打印没有任何边框的项目
Name ID
---------------------
Abc 21
我使用了这段代码
dgvmain.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
dgvmain.CellBorderStyle = DataGridViewCellBorderStyle.None;
其中dgvmain
是我的DataGridView
的名称
如有任何帮助,请提前致谢。
最佳答案
您需要通过向 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);
}
}
关于c# - 如何在 C# winforms 中设置自定义 DataGridView 边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18297083/