3

for example if I want to make certain column header hidden in Datagridview. I'll use this code:

dataGridView1.ColumnHeadersVisible = false;

But this will make all the column header to be invisible. Now what if I want only certain column header to be hidden.

For instance I have 3 column header. I just want the 3rd column header to be invisible without hidden all the rows data belongs to that column. Is that possible?

Please advise and correct me if I'm wrong.

Ren
  • 765
  • 4
  • 15
  • 42
  • Just the header or the entire column? This will hide the column: http://stackoverflow.com/questions/6960739/how-to-hide-column-of-datagridview-when-using-custom-datasource – Jane S May 16 '14 at 03:00
  • Not the entire column, just the header. – Ren May 16 '14 at 03:06
  • 1
    I'm now going to assume you mean clear the header text? Something like this may work: `grid.Columns[2].HeaderText = "";` – Jane S May 16 '14 at 03:09
  • @intracept if clear the header text, there's still a separator lines. – Ren May 16 '14 at 03:16
  • 1
    And there always will be if the column exists. "You ask the impossible!" - Luke Skywalker :) – Jane S May 16 '14 at 03:19
  • @intracept so it is impossible? – Ren May 16 '14 at 03:24
  • As far as I'm aware hiding the text is as close as you're going to get. – Jane S May 16 '14 at 03:27
  • Just to clarify, you want to remove/hide one column header, but not the column? so that will give you 2 column headers and 3 columns? – Ben May 16 '14 at 03:44
  • @Ben Yes, I did not want to remove. Just hide the 3rd column header. Therefore, there 2 column header and 3 columns for the row added. – Ren May 16 '14 at 03:48
  • Yeah I'd have to say that doesn't sound possible. I may be wrong, but I'd be surprised if I was. – Ben May 16 '14 at 04:16

3 Answers3

1

You will need to handle the DataGridView.CellPainting Event.

In the event handler you will be given an instance of DataGridViewCellPaintingEventArgs. You can use the ColumnIndex and RowIndex properties of this object to determine if a header cell you want to hide is being painted. RowIndex will be -1 if the cell being painted is a column header.

It may just be a simple matter of doing nothing except e.Handled = true; when the header cell in question is being painted.

Igby Largeman
  • 16,495
  • 3
  • 60
  • 86
1

I found this post searching for help with a similar problem. I was hiding specific columns (anything past column 5) in my grid from the RowDataBound event handler using:

for (int i = 6; i<e.Row.Cells.Count; i++)
{
    e.Row.Cells[i].Visible = false;
}

...which worked, but all the headers for those columns were still there. I was able to solve that by adding this line to the loop:

for (int i = 6; i<e.Row.Cells.Count; i++)
{
    e.Row.Cells[i].Visible = false;
    GridView1.HeaderRow.Cells[i].Visible = false;
}

Now the columns and headers are in sync. I'm not excited that I had to reference the GridView1 itself in the event handler, so if someone knows of a way to improve that, please go ahead.

I was eliminating everything after column 5, but you could use the same concept to hide any specific column/header. Hope that helps.

Doug
  • 11
  • 1
0

Just set the HeaderText of that column to the blank string.

Example: You want to hide the first column header:

myGridView.Columns[0].HeaderText = "";
Quan
  • 473
  • 7
  • 16