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

查看:883
本文介绍了如何设置在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
---------------------

和打印项目没有像这样

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天全站免登陆