?
Solved

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

Posted on 2012-12-27
7
Medium Priority
?
421 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 52

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 52

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
CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

771 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