如何在C#winforms中设置自定义DataGridView边框 [英] How to set custom DataGridView Borders in C# winforms

查看:204
本文介绍了如何在C#winforms中设置自定义DataGridView边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有任何机构可以帮助我打印datagridview标题行,那么下面的单个虚线就像:

I am stuck in this if any body can help i want to print the datagridview header row with a single dashed line below only like:

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

打印没有任何边框的项目

And print the items without anyborders like this

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

我使用这个代码

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

其中 dgvmain 是我的 DataGridView
任何帮助提前感谢。

Where dgvmain is the name of my DataGridView Any help thanks in advance.

推荐答案

通过向 CellPainting 事件处理程序添加代码来做一点自定义绘画。要将单元格边框设置为,请使用 CellBorderStyle

You need to do a little custom painting by adding code to the CellPainting event handler. For setting the cells border to None, use 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#winforms中设置自定义DataGridView边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆