4

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.

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

I do not want to hide columns or data which Contain my cells .

I just wanna hide a Specified cells.

dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
TaW
  • 53,122
  • 8
  • 69
  • 111

1 Answers1

8

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

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

Here is an example:

enter image description here

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.

Update:

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;
}

The CellPainting now looks like this:

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;

}

Update 2:

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

TaW
  • 53,122
  • 8
  • 69
  • 111
  • I call this method under a button but it signals a mistake. How can I call it properly under a button – Akpegnon K. Jacob Mahone's Sep 25 '18 at 19:27
  • It needs to work from the CellPainting event. What you can do in a button click event is set some data at class level, maybe, or in the cell.Tag, which you then can use in the checks in above code. I can give you an example but you need to be clearer what 'hiding' a cell means. Hiding a single is not possible at all. Hiding borders is, as you can see. But if you hide all four borders of a cell the result is a 'cross' of 5 cells. Is that what you want? – TaW Sep 25 '18 at 19:31
  • I want to hide The line of two cells from two colones to form a cell like what your image shows at the fourth column – Akpegnon K. Jacob Mahone's Sep 25 '18 at 19:49
  • I have updated the answer with a dynamic way for merging horizontally and or vertically.. – TaW Sep 25 '18 at 20:10
  • Thanks for the reply now I want to know how to use this event under a button can u give an example about use it Under buutton – Akpegnon K. Jacob Mahone's Sep 25 '18 at 20:13
  • Well call it like any other function! You need to know which cell you target and add false and true as paremeters for the merging you wanted : `mergeCells(dataGridView1[2,1], false, true);` – TaW Sep 25 '18 at 20:19
  • help about call function dataGridView1_CellPainting – Akpegnon K. Jacob Mahone's Sep 27 '18 at 00:46
  • You don't call it, it is an event and gets called by the system for each cell that needs to ne painted. You can trigger is with dgv.Invalidate(). – TaW Sep 27 '18 at 02:15
  • The cell if you wanted to use it but I suggest using `dgv.Invalidate();`. No params! – TaW Sep 27 '18 at 06:03
  • If you are happy with the answer, please consider consider [accepting](http://stackoverflow.com/help/accepted-answer) it..! - I see that you have never done this: Go the the (invisible) checkmark at the top left, below the votes of the answer and click it! It turns green and gains us both a little reputation.. – TaW Sep 27 '18 at 12:47
  • I see that you have never done this: Go the the (invisible) checkmark at the top left, below the votes of the answer and click it! It turns green and gains us both a little reputation>>>> I KNOW HOW TO MARK AS ANSWER – Akpegnon K. Jacob Mahone's Sep 27 '18 at 22:19
  • Good. Sorry if that came across the wrong way but so many newbies don't.. :-) – TaW Sep 28 '18 at 00:33