Solved

Datagridview columnheader image disappears on mouse hover

Posted on 2010-09-15
18
1,124 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
DevOps Toolchain Recommendations

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

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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