Results 1 to 8 of 8

Thread: [RESOLVED] Slow drawing DataGridView

  1. #1

    Thread Starter
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Posts
    3,250

    Resolved [RESOLVED] Slow drawing DataGridView

    I've got a DataGridView that I am populating the DataSource member with a DataSet object I've retrieved from SQL Server.

    The table I'm having the DataGridView display has 100 columns. That's quite a few columns (and yes, all of them are relevant and necessary). It takes the form an extra, noticeable 3 to 5 second delay to render.

    Does anybody have any suggestions as to decrease that lag and make it faster? It's only one row, so it's not row count problems, it's the column count being so high.

  2. #2
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: Slow drawing DataGridView

    FWIW, I've read that the time it takes ADO.NET to populate a dataset is the time you have to wait period, and there's nothing you can really do to speed it up. If you know what the user will be looking for, perhaps you could speed it up using a background worker to fetch data ahead of time. It takes me about 6 seconds or so to show 181 data fields to the user, through 160,000 possible records assuming no indexed fields.

    I will be interested to see any replies to this question...

  3. #3

    Thread Starter
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Posts
    3,250

    Re: Slow drawing DataGridView

    Quote Originally Posted by jayinthe813 View Post
    FWIW, I've read that the time it takes ADO.NET to populate a dataset is the time you have to wait period, and there's nothing you can really do to speed it up. If you know what the user will be looking for, perhaps you could speed it up using a background worker to fetch data ahead of time. It takes me about 6 seconds or so to show 181 data fields to the user, through 160,000 possible records assuming no indexed fields.

    I will be interested to see any replies to this question...
    Well, the thing is I'm not having issues populating the DataSet at all! It's past the data loading aspect and onto the binding / loading. I've got the loading done asynchronously and done before the form even shows. Other parts of my program push 10,000 rows at a time in sub second intervals. Granted it's only 15-ish columns compared to 100, but I've basically identified the slow part as the rendering.

  4. #4
    Member
    Join Date
    Mar 2009
    Posts
    61

    Re: Slow drawing DataGridView

    Hiya,
    I have also had issue's with gridview rendering, a massive improvement in performance was achieved after I used this guys advice..

    http://www.vbforums.com/showthread.p...bleLayoutPanel

    This will help with rendering speed..

    BR
    Phezo

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Slow drawing DataGridView

    Are the columns autosized? Recently I had a DataGridView with < 10 columns that took a noticeable amount of time to populate. After setting each columns width specifically it was dramatically faster.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Slow drawing DataGridView

    Are the columns AutoGenerated? If so, that is a lot of Reflection going on to define the datagridview columns.

    I suggest doing something like as follows to define the columns. Just create a new form and then paste this code to test this.

    VB.Net Code:
    1. Public Class Form1
    2.    Private WithEvents dgv As New DataGridView With {.Dock = DockStyle.Fill}
    3.    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    4.       Dim dt As New TableDef
    5.       Dim data As New List(Of String)(100)
    6.       For i As Int32 = 1 To 100
    7.          data.Add("Fred - " & i.ToString)
    8.       Next
    9.       dt.Rows.Add(data.ToArray())
    10.       SetDGVColumns(dgv, dt)
    11.       dgv.AllowUserToAddRows = False
    12.       dgv.AllowUserToDeleteRows = False
    13.       dgv.Parent = Me
    14.       dgv.DataSource = dt.DefaultView
    15.    End Sub
    16.  
    17.    Private Sub SetDGVColumns(ByVal dgv As DataGridView, ByVal dt As DataTable)
    18.       dgv.Columns.Clear()
    19.       dgv.AutoGenerateColumns = False
    20.       For Each col As DataColumn In dt.Columns
    21.          Dim gCol As DataGridViewColumn
    22.          ' Set this Case to whatever criteria that makes sense for you
    23.          Select Case Type.GetTypeCode(col.DataType)
    24.             Case TypeCode.Boolean
    25.                gCol = New DataGridViewCheckBoxColumn
    26.             Case Type.GetTypeCode(GetType(Byte()))
    27.                gCol = New DataGridViewImageColumn
    28.             Case Else
    29.                gCol = New DataGridViewTextBoxColumn
    30.          End Select
    31.          With gCol
    32.             .DataPropertyName = col.ColumnName
    33.             .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
    34.             .HeaderText = col.ColumnName
    35.             .DisplayIndex = col.Ordinal
    36.          End With
    37.          dgv.Columns.Add(gCol)
    38.       Next
    39.    End Sub
    40.  
    41.    Private Class TableDef
    42.       Inherits DataTable
    43.       Public Sub New()
    44.          With Columns
    45.             For i As Int32 = 1 To 100
    46.                .Add("C" & i.ToString, GetType(String))
    47.             Next
    48.          End With
    49.       End Sub
    50.    End Class
    51.  
    52. End Class

  7. #7
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Slow drawing DataGridView

    The DataGridView is a very heavyweight control. Extremely powerful and versatile. Unfortunately, along with databinding, this comes at a price.

    Certain aspects of the DataGridView, depending on what you are trying to do, can really slow things down, as you have experienced.

    Some may balk at this, but, if what you are displaying is very well defined, you could try 'rolling your own'. Instead of using the DataGridView, create your own user control that you draw on. Obviously, there's a lot of manual work, but the reward can be a much more responsive and dynamic control. It does require an understanding of what the user will do with the data (cut, copy, edit, etc.) which can seriously complicate matters, and really not make it worth the time.

    I'm not advocating such, but it is an option: I've displayed tabular data (with graphical representations) as hand drawn graphics and text which contains thousands of rows of information with no perceivable performance hindrance. The bottleneck is getting the data from the server (paged queries could help).
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  8. #8

    Thread Starter
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 2008
    Posts
    3,250

    Re: Slow drawing DataGridView

    Quote Originally Posted by SJWhiteley View Post
    The DataGridView is a very heavyweight control. Extremely powerful and versatile. Unfortunately, along with databinding, this comes at a price.

    Certain aspects of the DataGridView, depending on what you are trying to do, can really slow things down, as you have experienced.

    Some may balk at this, but, if what you are displaying is very well defined, you could try 'rolling your own'. Instead of using the DataGridView, create your own user control that you draw on. Obviously, there's a lot of manual work, but the reward can be a much more responsive and dynamic control. It does require an understanding of what the user will do with the data (cut, copy, edit, etc.) which can seriously complicate matters, and really not make it worth the time.

    I'm not advocating such, but it is an option: I've displayed tabular data (with graphical representations) as hand drawn graphics and text which contains thousands of rows of information with no perceivable performance hindrance. The bottleneck is getting the data from the server (paged queries could help).
    Thanks for the suggestion SJ. Thankfully, I don't think I'm going to need to go that far. What I ended up doing was changing 11 properties on the DataGridView and received HUGE performance gains:

    This tidbit is in C# by the way
    Code:
                    DataGridView oGridView = aControl as DataGridView;
                    oGridView.BorderStyle = BorderStyle.None;
                    oGridView.CellBorderStyle = DataGridViewCellBorderStyle.SingleVertical;
                    oGridView.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
                    oGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
                    oGridView.EditMode = DataGridViewEditMode.EditProgrammatically;
                    oGridView.EnableHeadersVisualStyles = false;
                    oGridView.AllowDrop = false;
                    oGridView.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
                    oGridView.ReadOnly = true;
                    oGridView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
                    oGridView.ShowCellToolTips = false;
    I noticed much faster load times and response times to the DataGridView, and since all I'm doing is just displaying ReadOnly data, this is perfect for me.
    Thanks everyone for your suggestions!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width