Link to home
Start Free TrialLog in
Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Datagridview columnheader image disappears on mouse hover

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?
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece image

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.
Also try to set EnableHeadersVisualStyles to false and try again ...
Avatar of RIAS

ASKER

oki...cheers will try and get back
Avatar of RIAS

ASKER

Hi,
Where is RowDatabound event on datagridview ?
Avatar of RIAS

ASKER

i am using vb.net 2005 windows .Is there any alternative for cell painting to add image in datagridview column header?
did you try to set EnableHeadersVisualStyles to false?
Avatar of RIAS

ASKER

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.
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/
rowDatabound is for GridView using System.Web.UI.WebControls Namespace
Avatar of RIAS

ASKER

rowDatabound is for GridView using System.Web.UI.WebControls Namespace
Can I use it in vb.net 2005 windows application?
ASKER CERTIFIED SOLUTION
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RIAS

ASKER

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 ?
 
 
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.
Avatar of RIAS

ASKER

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
Avatar of RIAS

ASKER

Cheers mate.Your solution is invaluable!!!!