|
-
Apr 4th, 2014, 10:41 PM
#1
[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.
-
Apr 4th, 2014, 10:58 PM
#2
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...
-
Apr 4th, 2014, 11:11 PM
#3
Re: Slow drawing DataGridView
 Originally Posted by jayinthe813
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.
-
Apr 5th, 2014, 09:33 AM
#4
Member
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
-
Apr 5th, 2014, 09:51 AM
#5
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.
-
Apr 5th, 2014, 10:40 AM
#6
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:
Public Class Form1
Private WithEvents dgv As New DataGridView With {.Dock = DockStyle.Fill}
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
Dim dt As New TableDef
Dim data As New List(Of String)(100)
For i As Int32 = 1 To 100
data.Add("Fred - " & i.ToString)
Next
dt.Rows.Add(data.ToArray())
SetDGVColumns(dgv, dt)
dgv.AllowUserToAddRows = False
dgv.AllowUserToDeleteRows = False
dgv.Parent = Me
dgv.DataSource = dt.DefaultView
End Sub
Private Sub SetDGVColumns(ByVal dgv As DataGridView, ByVal dt As DataTable)
dgv.Columns.Clear()
dgv.AutoGenerateColumns = False
For Each col As DataColumn In dt.Columns
Dim gCol As DataGridViewColumn
' Set this Case to whatever criteria that makes sense for you
Select Case Type.GetTypeCode(col.DataType)
Case TypeCode.Boolean
gCol = New DataGridViewCheckBoxColumn
Case Type.GetTypeCode(GetType(Byte()))
gCol = New DataGridViewImageColumn
Case Else
gCol = New DataGridViewTextBoxColumn
End Select
With gCol
.DataPropertyName = col.ColumnName
.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
.HeaderText = col.ColumnName
.DisplayIndex = col.Ordinal
End With
dgv.Columns.Add(gCol)
Next
End Sub
Private Class TableDef
Inherits DataTable
Public Sub New()
With Columns
For i As Int32 = 1 To 100
.Add("C" & i.ToString, GetType(String))
Next
End With
End Sub
End Class
End Class
-
Apr 7th, 2014, 09:22 AM
#7
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."
-
Apr 8th, 2014, 01:08 AM
#8
Re: Slow drawing DataGridView
 Originally Posted by SJWhiteley
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|