Solved

Datagridview columnheader image disappears on mouse hover

Posted on 2010-09-15
18
1,117 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?
0
Comment
Question by:RIAS
  • 8
  • 8
  • 2
18 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33680534
are your Column Headers Clickable?
0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33680535
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.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33680571
Also try to set EnableHeadersVisualStyles to false and try again ...
0
 

Author Comment

by:RIAS
ID: 33680758
oki...cheers will try and get back
0
 

Author Comment

by:RIAS
ID: 33680767
Hi,
Where is RowDatabound event on datagridview ?
0
 

Author Comment

by:RIAS
ID: 33680794
i am using vb.net 2005 windows .Is there any alternative for cell painting to add image in datagridview column header?
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33680803
did you try to set EnableHeadersVisualStyles to false?
0
 

Author Comment

by:RIAS
ID: 33680825
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.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33680845
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/
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33680860
rowDatabound is for GridView using System.Web.UI.WebControls Namespace
0
 

Author Comment

by:RIAS
ID: 33680886
rowDatabound is for GridView using System.Web.UI.WebControls Namespace
Can I use it in vb.net 2005 windows application?
0
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 500 total points
ID: 33680918
no you can not..DataGridView Is another object ...
You can translate if you want the code of the link i gave you in
http://www.developerfusion.com/tools/convert/csharp-to-vb/
Translate it step by step .not all the code together
0
 

Author Comment

by:RIAS
ID: 33680974
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 ?
 
 
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33680992
a simple code when you ha ve a picture box for example is :
PictureBox1.Image = ImageList1.Images.Item(0)
0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33681043
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.
0
 

Author Comment

by:RIAS
ID: 33681248
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
0
 

Author Closing Comment

by:RIAS
ID: 33682091
Cheers mate.Your solution is invaluable!!!!
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33682157
Very Welcome!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SlingPlayer alternatives 1 36
scanning dentists xray (the small ones) 3 45
Advice in Xamarin 21 57
Where can I ask about my ASP.NET MVC project? 6 23
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now