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

标签 c# .net winforms datagridview

如果任何机构可以帮助我想用下面的单虚线打印 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);
    }
}

enter image description here

关于c# - 如何在 C# winforms 中设置自定义 DataGridView 边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18297083/

相关文章:

c# - C#套接字服务器发送问题

c# - 如何让 ContextMenuStrip 在左键单击 NotifyIcon 时显示?

c# - 您可以将泛型类型的实例传递给其他强类型方法吗?

c# - INotifyPropertyChanged/绑定(bind)?

c# - 使电话连接到调制解调器振铃

c# - 如何避免在变量赋值的情况下调用冗余?

c# - 系统.IO.FileLoadException : Could not load file or assembly Log4net

c# - "cannot implement interface member"接口(interface)和混凝土在不同项目时的错误

c# - 如何在满足条件之前禁用控件?

c# - ProxyFactoryFactory 未配置