Solved

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

Posted on 2012-12-27
7
410 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
  • 4
  • 2
7 Comments
 
LVL 48

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 48

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now