Solved

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

Posted on 2012-12-27
7
413 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 49

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 49

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Single Form 8 29
Visual Studio TFS - how do I check in my code? 2 30
How to join on ID, with prefix? 15 17
Regex validation 2 0
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

864 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

19 Experts available now in Live!

Get 1:1 Help Now