在datagridview中隐藏指定的单元格边框? [英] Hide specified cell borders in datagridview?

查看:24
本文介绍了在datagridview中隐藏指定的单元格边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的数据视图中隐藏一两个网格单元格.但是使用这段代码,所有的网格都被隐藏了,这不是我想要的.

I want to hide one or two grid cells from my datagriview. But with this code all the grids are hidden and this is not what I want.

我想从我的 Datagridview 中隐藏一两个矩形单元格.

I want to hide one or two rectangles cells from my Datagridview.

我只想隐藏一个指定的单元格.

I just wanna hide a Specified cells.

dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;

推荐答案

推荐的隐藏或修改单元格边框样式的方法是编写 CellPainting 事件.

The recommended way to hide or modify cell border style is to code the CellPainting event.

别担心,不需要实际绘画.您需要做的就是在 e.AdvancedBorderStyle 参数中设置几个字段.

Don't worry, no actual painting is required. All you need to do is set a few fields in the e.AdvancedBorderStyle parameter.

这是一个例子:

注意第三列中单元格的垂直合并"外观;底部的水平合并"单元格也是如此.也是第 5 列单元格的双边框.

Note the 'vertically merged' look of of the cells in the 3rd column; same for the 'horizontally merged' cells at the bottom. Also the double border of a cell in the 5th column.

private void dataGridView1_CellPainting(object sender,
                                        DataGridViewCellPaintingEventArgs e)
{
    if (e.ColumnIndex == 2 && e.RowIndex == 6)
        e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None;

    if (e.ColumnIndex == 2 && e.RowIndex == 1)
        e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;

    if (e.ColumnIndex == 4 && e.RowIndex == 4)
    {
        e.AdvancedBorderStyle.All = DataGridViewAdvancedCellBorderStyle.InsetDouble;
        e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Single;
    }
}

请注意,隐藏边框相当简单:只需隐藏右侧或底部边框;其他边框样式需要一些试验和错误(或更深入的理解;-)

Note that hiding borders is rather straight forward : Simply hide the right or the bottom border; other borderstyles require some trial and error (or a deeper understanding ;-)

在这里我首先设置所有边的样式,但是当它把底部涂成白色(至少我认为它是这样)然后我将底部边框设置回单一.

Here I first set the style for all sides but as it paints the botton white (at least that's what I think it does) I then set the botton border back to single.

您可能希望简化检查的方式;这只是一个简单的例子.

You may want to streamline the way the checks are done; this is just a simple example.

更新:

这是使合并更加动态的代码:使用 mergeCells 函数标记一个单元格,以便与其右侧或底部邻居合并或取消合并:

Here is a code to make the merging more dynamic: Use the mergeCells function to mark a cell for merging or un-merging with its right or bottom neighbour:

private void mergeCells(DataGridViewCell cell, bool mergeH, bool mergeV)
{
    string m = "";
    if (mergeH) m += "R";  // merge horizontally by hiding the right border line
    if (mergeV) m += "B"; // merge vertically by hiding the bottom border line
    cell.Tag =  m == "" ? null : m;
}

CellPainting 现在看起来像这样:

private void customDGV1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
    DataGridViewCell cell = ((DataGridView)sender)[e.ColumnIndex, e.RowIndex];
    if (cell.Tag == null) return;
    string hide = cell.Tag.ToString();

    if (hide.Contains("R")) 
        e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None;
    else
        e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.Single;

    if (hide.Contains("B")) 
        e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
    else 
        e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Single;

}

更新 2:

如果你想把它应用到 ColumnHeaders 你需要先关闭 dgv.EnableHeadersViualStyles..

If you want to apply this to the ColumnHeaders you need to turn off dgv.EnableHeadersViualStyles first..

这篇关于在datagridview中隐藏指定的单元格边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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