Solved

Datagridview columnheader image disappears on mouse hover

Posted on 2010-09-15
18
1,114 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VB.net PaintEventArgs Handling. 10 28
.Net application crashing 6 21
Create XML 5 34
Recommendation vb6 to vb.net or others 14 44
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

8 Experts available now in Live!

Get 1:1 Help Now