Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

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

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 53

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 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