We help IT Professionals succeed at work.

Datagridview columnheader image disappears on mouse hover

RIAS
RIAS asked
on
1,259 Views
Last Modified: 2013-11-07
Hi,
I have added a image in datagridview columnheader on cell painting event

    Private Sub Dg1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles Dg1.CellPainting

        AddTickToSelectionCol(sender, e) 'Add a tick image on the first column of grid
    End Sub

Public Sub AddTickToSelectionCol(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
        If e.ColumnIndex = 0 AndAlso e.RowIndex = 0 Then
            e.PaintBackground(e.ClipBounds, False)

            Dim pt As Point = e.CellBounds.Location
            Dim offset As Integer = (e.CellBounds.Width - Me.ImageList1.ImageSize.Width) / 2

            pt.X += offset
            pt.Y = 1
            Me.ImageList1.Draw(e.Graphics, pt, 0)

            e.Handled = True

        End If
    End Sub

The problem is that it disappears on mousehover on the column header.
Any suggestions?
Comment
Watch Question

John (Yiannis) ToutountzoglouInstructor Multiengine Pilot

Commented:
are your Column Headers Clickable?
Can you write this code on RowDatabound event?

there you can apply a check for header row.

paint event again fire when you mouse over on header, that why during mouse over, it again n again n again try to paint/add tick on header, that's why its looks like that disappear.
John (Yiannis) ToutountzoglouInstructor Multiengine Pilot

Commented:
Also try to set EnableHeadersVisualStyles to false and try again ...

Author

Commented:
oki...cheers will try and get back

Author

Commented:
Hi,
Where is RowDatabound event on datagridview ?

Author

Commented:
i am using vb.net 2005 windows .Is there any alternative for cell painting to add image in datagridview column header?
John (Yiannis) ToutountzoglouInstructor Multiengine Pilot

Commented:
did you try to set EnableHeadersVisualStyles to false?

Author

Commented:
Hi jtoutou,
Did try it and it worked.Just looking for an alternative for cell paint as VjSoft suggested it keeps on firing on and off.
If I get no answer to the alternative I will surely accept your solution.Cheers for that.
John (Yiannis) ToutountzoglouInstructor Multiengine Pilot

Commented:
no....there is an implementation also using Cell Paint...
Take a look here it is a large code in C ...
http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/daaf033d-8160-4990-ae7e-8d8af20426b9/
John (Yiannis) ToutountzoglouInstructor Multiengine Pilot

Commented:
rowDatabound is for GridView using System.Web.UI.WebControls Namespace

Author

Commented:
rowDatabound is for GridView using System.Web.UI.WebControls Namespace
Can I use it in vb.net 2005 windows application?
Instructor Multiengine Pilot
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
oki trying that..cheers
Struggling on this line
Dim directoryImage As Bitmap = Global.DataGridViewImageHeaderCellElementsSample.Properties.Resources.Directory
 Any ideas on how to pick a image from imagelist ?
 
 
John (Yiannis) ToutountzoglouInstructor Multiengine Pilot

Commented:
a simple code when you ha ve a picture box for example is :
PictureBox1.Image = ImageList1.Images.Item(0)
Sorry for late response, you can find RowDataBound for gridview from below url
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

You can generate this events by open page designer mode, select grid view, right click on it check properties, go into event tab, there you will find rowdatabound event.

Author

Commented:
Hi jtoutou,
Converted it but on implementation this line returns nothing
Dim dataGridViewImageColumnHeaderCell As DataGridViewImageColumnHeaderCell = TryCast(dataGridViewTextBoxColumn.HeaderCell, DataGridViewImageColumnHeaderCell)
 
immediate window : ? dataGridViewImageColumnHeaderCell  return : nothing
I know this slightly out of question raised but if you can suggest anything it will be great  Cheers

Author

Commented:
Cheers mate.Your solution is invaluable!!!!
John (Yiannis) ToutountzoglouInstructor Multiengine Pilot

Commented:
Very Welcome!!!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.