Solved

Datagridview columnheader image disappears on mouse hover

Posted on 2010-09-15
18
1,129 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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
 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 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