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

问题描述

我想从我的datagriview中隐藏一个或两个网格单元。但是有了这些代码,所有的网格都被隐藏了,这不是我想要的。

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.

这里是一个示例:

请注意第3列中单元格的垂直合并外观;底部的水平合并单元格也是如此。也是第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中的指定单元格边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 13:13