Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this icon on the Header of my dataGridView1
But also the header text as well.
CLICK THIS IMAGE: https://i.imgur.com/rQuoMNv.jpg[^]

What I have tried:

I did already:
C#
dataGridView1.Columns[6].HeaderText = "";

But is not convenient, because NOW I need to :

C#
//Save all the Headers first
string h8 = "";
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                h8 += dataGridView1.Columns[i].HeaderText + "\\";
            }

and I get that "" nothing string from dataGridView1.Columns[6].HeaderText.
I like to have the Name of the Header in there, also because I get that baloon, very helpful.
So my question is:
- How to keep the text of the dataGridView Header but hide it only Visually.
Also not to screw the image that is on top of it either.
Posted
Updated 1-Nov-21 3:47am
v3
Comments
phil.o 1-Nov-21 8:11am    
Better than playing with displayed text, it would be better to modify the code which inserts an icon in a header cell, which you did not show.
_Q12_ 1-Nov-21 8:32am    
Somewhereelseincode: dataGridView1.Columns[6].HeaderText = "HomeInventory";
then...
Bitmap img1 = new Bitmap(@"instock.png");
private void dataGridView1_CellPainting_1(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == dataGridView1.Columns.Count - 1 && e.RowIndex == -1)
{
Rectangle cellRectangle = dataGridView1.GetCellDisplayRectangle(6, -1, true);
e.PaintBackground(e.ClipBounds, false);
e.Graphics.DrawImage(img1, cellRectangle.Location.X + 5, 1, 18, 18);
}
}
As you can observe, Im only dealing with 1 header cell, #6 Columns[6].Header

As far as I know, there is no way to have a HeaderText without it being visible - which means it will "intrude" on your image.

And the header cell isn;t derived from Control either, so it doesn't have a Tag property.

But ... you could cheat.
There is a ToolTipText property which you could set, and use that instead of the HeaderText.
If you don't include a Tooltip on your form, then it'll never be displayed any other way, and if you do, it'll show the header text to "back up" the image for the user if he isn't sure what the image means.
 
Share this answer
 
Comments
_Q12_ 1-Nov-21 8:47am    
My temporary solution... and probably final but I still wait for your final answers.
I leave back as it was: dataGridView1.Columns[6].HeaderText = "";
I let it do it's thing:
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
h8 += dataGridView1.Columns[i].HeaderText + "\\";
}
h8 = h8.Remove(h8.Length - 1);//remove last "\"
And cheatingly (like you suggested) I did this:
h8 = h8.Replace("\\\\", "\\HomeInventory\\");
Not my proudest code but.. like i said, works as a temporary solution, and will remain as that if nothing else will be found.
But I still want a better solution from you! (when you can)
_Q12_ 1-Nov-21 8:56am    
I succesfully used your Tooltip cheat !
After my implementation, I didnt had the tooltip option anymore (by default).
So reading again your solution there, I changed the tooltip property, and NOW, I have that baloon back - Super awesome !
Thank you very much mister OriginalGriff !!!
OriginalGriff 1-Nov-21 9:16am    
You're welcome!
You are clearing the Text of a specific Column's header.

At design-time you can set all Headers visibility in the Property Browser: [^]

At run-time you can set the property in code: dataGridView1.ColumnHeadersVisible = false;

To set a specific Column header visibility:

0) you will find 'visible properties on row[#n].Cells[0] header cells, and on on column[#n] headers, but, those properties are read-only.

1) clear the Text, but save the Text first, restore it later, as necessary

1a) write code to record the current Columm Header Text properties when your app loads.

1b) create a map of Column Header Text values.

1c) write a function to clear or restore a specific Column's Header Text using 1a( and 1b)

1d) see code ideas below

2) set the specific column's header Cell visibility [^] (not recommended unless you are an advanced WinForm programmer).

Code ideas ... note: i do not provide full working code solutions because i am here to teach, and, if you learn to write the missing code, you will learn something valuable you can re-use in the future. If you get started using these code ideas, and ask specific questions about the code you show me, i'll be happy to respond.

1) to save the values of all current Columns' header Text; you write the code to initialize this List in the appropriate place:

private List<string> CurrentColumnHeaderText = new List<string>();

2) you write functions like these;

private void UpdateSavedHeaderText(DataGridViewColumn col)
{   
}

publicvoid SetColHeaderVisibility(int columnndx, bool RestoreOrClear)
{
    DataGridViewColumn col = dataGridView1.Columns[columnndx];

    if (RestoreOrClear)
    {

        // restore
    }
    else
    {
        // if you changed header text at run-time some other way ... ?
        // update the current Text here

        if (CurrentColumnHeaderText[col.Index] != col.HeaderText)
        {
            UpdateSavedHeaderText(col);
        }

        // clear
    }

    dataGridView1.Invalidate();
}

private void RestoreHeaderText(DataGridViewColumn column)
{
}
 
Share this answer
 
Comments
_Q12_ 2-Nov-21 12:25pm    
Thank you
BillWoodruff 2-Nov-21 12:34pm    
you're welcome :) i find myself wondering why you would want to hide a specific ColumnHeader Text. i can imagine changing the column header text for various reasons. cheers, bill
_Q12_ 2-Nov-21 12:39pm    
BillWoodruff said:i find myself wondering why you would want to hide a specific ColumnHeader Text.
q12:
Read the very top original question:
"I have this icon on the Header of my dataGridView1
But also the header text as well.
CLICK THIS IMAGE: https://i.imgur.com/rQuoMNv.jpg[^]"
Click that link I provided and you will understand my reason.
Cheers.
BillWoodruff 2-Nov-21 14:54pm    
Okay, i can see that, now :) Have you explored the possibility of defining your own custom DataGridViewColumnHeaderCell ? While i would override CellPaint ... because i often have several reasons for doing that ... i would check out: (i have not tried it)

https://stackoverflow.com/a/24824261/133321

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900