Solved

Visual Basic - Add line(s) on individual rows in datagridview - Flickering

Posted on 2012-12-27
7
416 Views
Last Modified: 2013-01-19
I have a datagridview in my application (at this moment Visual Basic 2008, but in the near future Visual Basic 2010) and I want to create line(s) in individual rows.

At this moment I use the following code:

Private Sub datagridview1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles datagridview1.CellPainting

        Dim row As DataGridViewRow = datagridview1.Rows(e.RowIndex)
               
        Dim BackColorBrush As New SolidBrush(e.CellStyle.BackColor)
       	Dim GridBrush As New SolidBrush(Me.datagridview1.GridColor)
        Dim GridLinePen As New Pen(GridBrush)

      	' -- Erase the cell
       	e.Graphics.FillRectangle(BackColorBrush, e.CellBounds)
                     
      	Dim ProgressBarBrush As New SolidBrush(Color.Black)
      	Dim CellProgressBarRect As New Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, 2)
       	e.Graphics.FillRectangle(ProgressBarBrush, CellProgressBarRect)

     	e.Handled = True
                        
      	Dim style As New DataGridViewCellStyle
      	style.Font = New Font(datagridview1.Font, FontStyle.Bold)
      	datagridview1.Rows(e.RowIndex).DefaultCellStyle = style

End Sub

Open in new window


It works fine, but I have one problem:

The line(s) are 'flickering' on my screen. I know it happened by the CellPainting, but I don't know how to fix that. Does anyone know how to fix that?
0
Comment
Question by:Program1981
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 38723534
HI,

This could help
Public Sub SuspendLayout
'Usage
Dim instance As Control

instance.SuspendLayout
' your code
instance.ResumeLayout

Open in new window

0
 

Author Comment

by:Program1981
ID: 38723582
Thank you for your answer! Can you be a little bit more specific?

What do I have to do with my cellpainting code?

The datagridview is filled by a sql query (fill a datatable). I added columns manually and connect the datagridview to a datatable (datasource = datatable) in the form_load.
0
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 38723657
Hi,

You can try SuspendLayout () before initializing your form and ResumeLayout () after initialization.  Sometimes this doesn't work.  When it doesn't, you can try using the Win32 functions:

Outside of your method:

using Microsoft.Win32;
using System.Runtime.InteropServices;


        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
        private const int WM_SETREDRAW = 11;

 

Before drawing:

            SendMessage(ObjectControlPanel.Handle, WM_SETREDRAW, false, 0);


After drawing:

            SendMessage(ObjectControlPanel.Handle, WM_SETREDRAW, true, 0);


You may then need to call Refresh ().

refer to http://www.c-sharpcorner.com/Forums/Thread/52/

Regards
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

Author Comment

by:Program1981
ID: 38723945
I tried this methods and it changes nothing unfortunately.

The flickering ("winking") still exist in the line(s).
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38724459
Can you show us a screenshot so we see what is the outcome of above code?

One change you can make immediately and which might help is to remove the last 3 lines and add following to form_load


datagridview1.Rows(e.RowIndex).DefaultCellStyle.Font = New Font(datagridview1.Font, FontStyle.Bold)
0
 

Accepted Solution

by:
Program1981 earned 0 total points
ID: 38777251
This problem is solved. I add some empty lines with black background.
This is for me a good solution.
0
 

Author Closing Comment

by:Program1981
ID: 38796113
I tried something and this is for me a good solution.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…

734 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