none
Background image in Winform Datagrid RRS feed

  • Question

  • I need to set background image in datagridview (win-forms).

    With help of this articles, ( http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/f61eb53b-3679-4444-8da0-48fc8b4dfd15

    & http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/047eaba9-016d-4570-84b6-78492907aacb)

    I am able to set background image.

    My problem is, when New row is added or removed, entire grid is flickering,

    also , on cell clicked or cell double clicked, image is flickering .

    What will be solution for this?

    Tuesday, December 18, 2012 6:10 AM

Answers

  • Hi Harsh,

    I can reproduce this flickering using your custom datagridview.

    I noticed that you didn't set the DoubleBuffered property. You can try to use

    Me.DoubleBuffered = True

    in InitializeComponent() method, and remove deleted and repaint condition to see whether it works.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 1, 2013 6:32 AM
    Moderator

All replies

  • Hi Harsh10,

    Thanks for your post.

    The grid is flickering when row is added or removed. Does the row added using code or editting in the new row? If the rows are added or deleted programmatically. Try to use SuspendLayout() and ResumeLayout() method. I have tested and I doesn't have this kind of flickering, so I don't know if it is useful to you.

    DataGridView doesn't support background by default. If we paint the background programmatically, we should also Invalidate every time when scrolling. I think this kind of flickering is difficult to be eliminated.

    Best regards


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Harsh10 Friday, December 21, 2012 12:25 PM
    • Unmarked as answer by Harsh10 Friday, December 21, 2012 12:25 PM
    Friday, December 21, 2012 2:57 AM
    Moderator
  • Hi Chester,

    Thanks for Reply.

    In my code, rows are added or deleted by code, not by editing.

    I have Checked with SuspendLayout() and ResumeLayout() methods, but this does not help me.

    I don't have any issue with scrolling of grid.  There is one solution for scrolling in one of link i have mentioned in original question.

    Regards.

    Harsh

    Friday, December 21, 2012 7:48 AM
  • Hi Harsh,

    Thanks for the information.

    I have tried the method to use Invalidate() method in ValueChanged event handler in the first link. It resolves the flickers when scrolling. But unfortunately I don't have the issue when adding and deleting rows.

    I think you can try to add handler to RowsAdded and RowsRemoved event and use Invalidate() method to see whether it is useful.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 25, 2012 3:05 AM
    Moderator
  • Hi Chester,

    Thanks for Reply.

    InValidate() in RowsAdded and RowsRemoved event was not helpful to me.

    However, I find out that If

    MyBase.PaintBackground(graphics, clipBounds, gridBounds)

    this line we skipped during RowsAdded , it does not flickers while Adding Rows.

    So, Right now I have created some flags and during insert , above line is skipped by condition.

    Using this, half problem is resolved, still problem in deleting rows.

    However, there must be some good solution for this.

    Regards,

    Harsh


    • Edited by Harsh10 Tuesday, December 25, 2012 10:21 AM
    Tuesday, December 25, 2012 10:20 AM
  • Hi Harsh,

    Thanks for the information.

    Are you integrating the code from both the two threads and does the flickering occurs in the transparent cell's background?

    If not, would you like to provide the code of the DGV you are using so I can understand the issue better.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, December 27, 2012 9:42 AM
    Moderator
  • Hi Chester,

    This is my code of my inherited gridview.

    Public Class usrDataGridView
        Inherits Windows.Forms.DataGridView
    
        Private _backgroundPic As Image
    
        Private repaint As Boolean = False
        Private deleted As Boolean = False
    
        <Browsable(True)> _
        Public Overrides Property BackgroundImage() As Image
            Get
                Return _backgroundPic
            End Get
            Set(ByVal value As Image)
                _backgroundPic = value
            End Set
    
        End Property
    
        Protected Overrides Sub PaintBackground(ByVal graphics As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle, ByVal gridBounds As System.Drawing.Rectangle)
            If (Not (Me.BackgroundImage Is Nothing)) Then
                If (deleted) Then
                    deleted = False
                    ' MyBase.PaintBackground(graphics, clipBounds, gridBounds) '
                    repaint = False
                End If
                If (repaint) Then
                    Exit Sub
                Else
                    repaint = True
                End If
                graphics.DrawImage(Me.BackgroundImage, gridBounds)
            End If
    
        End Sub
    
        Public Sub SetCellsTransparent()
            Me.EnableHeadersVisualStyles = False
            Me.ColumnHeadersDefaultCellStyle.BackColor = Color.Transparent
            Me.RowHeadersDefaultCellStyle.BackColor = Color.Transparent
        End Sub
    
        Private Sub InitializeComponent()
            CType(Me, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.SuspendLayout()
            CType(Me, System.ComponentModel.ISupportInitialize).EndInit()
            Me.ResumeLayout(False)
    
        End Sub
    
        Private Sub usrDataGridView_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
            Me.Invalidate()
            repaint = False
        End Sub
        Private Sub usrDataGridView_RowsRemoved(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles Me.RowsRemoved
            deleted = True
        End Sub
    
    End Class

    In my form load event, following statements are performed in object of this usrDataGridView.

            DataGridView1.BackgroundImage = m_Image
            DataGridView1.SetCellsTransparent()
            DataGridView1.DataSource = dt
            DataGridView1.Invalidate()

    In this form, rows add and remove operations are performed on data-table (not on data-grid).

    In this code, it is not flickering while adding row, but flickers on deleting rows.

    If we remove code of deleted in datagridview class, it will stop flickering, but row is not removed from design.

    Regards,

    Harsh





    • Edited by Harsh10 Monday, December 31, 2012 5:45 AM
    Monday, December 31, 2012 5:45 AM
  • Hi Harsh,

    I can reproduce this flickering using your custom datagridview.

    I noticed that you didn't set the DoubleBuffered property. You can try to use

    Me.DoubleBuffered = True

    in InitializeComponent() method, and remove deleted and repaint condition to see whether it works.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 1, 2013 6:32 AM
    Moderator
  • Hi Harsh,

    I would like to mark this thread as answered.

    If this does not help you to solve the problem, please feel free to unmark it.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 9, 2013 10:08 AM
    Moderator