-
May 18th, 2013, 01:04 AM
#1
Thread Starter
Addicted Member
Formatting is slow in DataGridView than MSFlexgrid
Hi,
We have updated our application from VB 6 to VB.Net. We are using VS 2008. Now we are trying to convert MSFlexgrid used in our application to DataGridView. We are testing speed of both controls and we noticed that DataGridView is very much slow than MSFlexgrid while formatting cells(Changing background and foreground colour, making text bold etc).Its hard to use DataGridView if it is much slow. Why is DataGridView is slow while formatting? Is there any way to speed up?
Thank you
-
May 18th, 2013, 11:56 AM
#2
Re: Formatting is slow in DataGridView than MSFlexgrid
What exactly constitutes much slower? I can't say I've ever encountered any performance problems with the DGV itself so it's much more likely that any problems are in what you're doing with it and how you're doing it. Since you've chosen to give no information on that whatsoever it's next to impossible to suggest any improvements.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 19th, 2013, 11:37 PM
#3
Thread Starter
Addicted Member
Re: Formatting is slow in DataGridView than MSFlexgrid
Originally Posted by dunfiddlin
What exactly constitutes much slower? I can't say I've ever encountered any performance problems with the DGV itself so it's much more likely that any problems are in what you're doing with it and how you're doing it. Since you've chosen to give no information on that whatsoever it's next to impossible to suggest any improvements.
Ok, here is code i have used
Code:
Try
Dim i As Integer
Dim j As Integer
Dim txtToAppend As String = ""
Dim dstime As New DataSet
Dim sqlcmd As DbCommand
Dim rowcnt As Integer = InputBox("Please Enter RowCount", "DataSource")
txtToAppend = DateAndTime.Timer
Debug.Print(DateAndTime.Timer)
sqlcmd = CommonObj.CommandObj(True)
sqlcmd.Connection = getcon("Collections_Closed")
sqlcmd.CommandText = "select top(" & rowcnt & ") * from collections"
adapter.SelectCommand = sqlcmd
adapter.Fill(dstime, "Collections")
With DtdgDataSource
.SuspendLayout()
.DataSource = Nothing
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.AllowUserToResizeRows = False
.AllowUserToOrderColumns = True
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.ReadOnly = True
.MultiSelect = False
.RowHeadersVisible = False
.Columns.Clear()
.Rows.Clear()
.VirtualMode = True
End With
DtdgDataSource.GetType.InvokeMember("DoubleBuffered", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.SetProperty, Nothing, DtdgDataSource, New Object() {True})
DtdgDataSource.DataSource = dstime.Tables("Collections")
For i = 0 To (rowcnt / 2)
DtdgDataSource.Rows(i).DefaultCellStyle.BackColor = Color.LemonChiffon
Next
For i = 0 To DtdgDataSource.RowCount - 2
For j = 2 To 7
DtdgDataSource.Rows(i).Cells(j).Style.ForeColor = Color.DarkOrange
Next
Next
For i = 0 To DtdgDataSource.RowCount - 2
For j = 8 To 13
DtdgDataSource.Rows(i).Cells(j).Style.Font = New Font(DtdgDataSource.Font, FontStyle.Bold)
Next
Next
Catch ex As Exception
MsgBox(Err.Description & vbTab & "Button3_Click")
End Try
-
May 19th, 2013, 11:39 PM
#4
Thread Starter
Addicted Member
Re: Formatting is slow in DataGridView than MSFlexgrid
Originally Posted by dunfiddlin
What exactly constitutes much slower? I can't say I've ever encountered any performance problems with the DGV itself so it's much more likely that any problems are in what you're doing with it and how you're doing it. Since you've chosen to give no information on that whatsoever it's next to impossible to suggest any improvements.
Ok, here is code i have used
Code:
Try
Dim i As Integer
Dim j As Integer
Dim txtToAppend As String = ""
Dim dstime As New DataSet
Dim sqlcmd As DbCommand
Dim rowcnt As Integer = InputBox("Please Enter RowCount", "DataSource")
txtToAppend = DateAndTime.Timer
Debug.Print(DateAndTime.Timer)
sqlcmd = CommonObj.CommandObj(True)
sqlcmd.Connection = getcon("Collections_Closed")
sqlcmd.CommandText = "select top(" & rowcnt & ") * from collections"
adapter.SelectCommand = sqlcmd
adapter.Fill(dstime, "Collections")
With DtdgDataSource
.SuspendLayout()
.DataSource = Nothing
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.AllowUserToResizeRows = False
.AllowUserToOrderColumns = True
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.ReadOnly = True
.MultiSelect = False
.RowHeadersVisible = False
.Columns.Clear()
.Rows.Clear()
.VirtualMode = True
End With
DtdgDataSource.GetType.InvokeMember("DoubleBuffered", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.SetProperty, Nothing, DtdgDataSource, New Object() {True})
DtdgDataSource.DataSource = dstime.Tables("Collections")
For i = 0 To (rowcnt / 2)
DtdgDataSource.Rows(i).DefaultCellStyle.BackColor = Color.LemonChiffon
Next
For i = 0 To DtdgDataSource.RowCount - 2
For j = 2 To 7
DtdgDataSource.Rows(i).Cells(j).Style.ForeColor = Color.DarkOrange
Next
Next
For i = 0 To DtdgDataSource.RowCount - 2
For j = 8 To 13
DtdgDataSource.Rows(i).Cells(j).Style.Font = New Font(DtdgDataSource.Font, FontStyle.Bold)
Next
Next
Catch ex As Exception
MsgBox(Err.Description & vbTab & "Button3_Click")
End Try
Please see if any thing i am misusing or any improvements can be made
Thank you
-
May 20th, 2013, 04:06 AM
#5
Re: Formatting is slow in DataGridView than MSFlexgrid
For i = 0 To DtdgDataSource.RowCount - 2
For j = 8 To 13
DtdgDataSource.Rows(i).Cells(j).Style.Font = New Font(DtdgDataSource.Font, FontStyle.Bold)
Next
Next
The above code will create a new FONT object for each row, which will be slow.
Not sure if this would make a difference, but just give it a try:
Code:
Dim myFont as New Font(DtdgDataSource.Font, FontStyle.Bold)
For i = 0 To DtdgDataSource.RowCount - 2
For j = 8 To 13
DtdgDataSource.Rows(i).Cells(j).Style.Font = myFont
Next
Next
Last edited by Pradeep1210; May 20th, 2013 at 04:12 AM.
-
May 20th, 2013, 04:23 AM
#6
Re: Formatting is slow in DataGridView than MSFlexgrid
Or, try this:
(not tested code)
Code:
Dim myFont As New Font(DtdgDataSource.Font, FontStyle.Bold)
For j = 8 To 13
DtdgDataSource.Columns(j).DefaultCellStyle.Font = myFont
Next
-
May 20th, 2013, 07:14 AM
#7
Thread Starter
Addicted Member
Re: Formatting is slow in DataGridView than MSFlexgrid
Originally Posted by Pradeep1210
The above code will create a new FONT object for each row, which will be slow.
Not sure if this would make a difference, but just give it a try:
Code:
Dim myFont as New Font(DtdgDataSource.Font, FontStyle.Bold)
For i = 0 To DtdgDataSource.RowCount - 2
For j = 8 To 13
DtdgDataSource.Rows(i).Cells(j).Style.Font = myFont
Next
Next
Thanks for reply
Your suggestion will fasten the execution little,but its not enough,its still much slower than FlexGrid
-
May 20th, 2013, 08:49 AM
#8
Re: Formatting is slow in DataGridView than MSFlexgrid
What about post #6. Did you try that code?
I'm on a system at present where I can't test that code. But if that works, it would be much faster since it gets rid of the row-level looping. You may probably need to do that before binding the grid to datasource or it may work after data-binding.. not sure.
-
May 20th, 2013, 11:01 AM
#9
Re: Formatting is slow in DataGridView than MSFlexgrid
All the looping is taking the time away, especially the double loops. A lot of these changes can be made with a single command by using the columns as the basic unit rather than the cell, eg.
DataGridView1.Columns(0).DefaultCellStyle.ForeColor = Color.Orange
Then if you want single cells to vary from the column default you can do that for the individual cells rather than looping through setting every one. I have to say I don't really understand the purpose of..
For i = 0 To (rowcnt / 2)
DtdgDataSource.Rows(i).DefaultCellStyle.BackColor = Color.LemonChiffon
Next
Why would you want to divide the DGV in half arbitrarily?
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 20th, 2013, 11:34 PM
#10
Thread Starter
Addicted Member
Re: Formatting is slow in DataGridView than MSFlexgrid
Originally Posted by Pradeep1210
What about post #6. Did you try that code?
I'm on a system at present where I can't test that code. But if that works, it would be much faster since it gets rid of the row-level looping. You may probably need to do that before binding the grid to datasource or it may work after data-binding.. not sure.
Thanks for reply
yes,that code fasten the execution a lot.But my problem is we are using same loops for flexgrid also,and its faster than DataGridView for same code. That i need to understand.Is there anything extra to do with DataGridView to speed up? And if i use DtdgDataSource.Rows(i).Cells(j).Style.Font = myFont the font setting will be applied to whole column, i cant do that if i need setting for only some rows of column. In latter case i have to use DtdgDataSource.Rows(i).Cells(j).Style.Font = myFont i think
Last edited by winman; May 26th, 2013 at 11:41 PM.
-
May 20th, 2013, 11:35 PM
#11
Thread Starter
Addicted Member
Re: Formatting is slow in DataGridView than MSFlexgrid
Originally Posted by dunfiddlin
All the looping is taking the time away, especially the double loops. A lot of these changes can be made with a single command by using the columns as the basic unit rather than the cell, eg.
DataGridView1.Columns(0).DefaultCellStyle.ForeColor = Color.Orange
Then if you want single cells to vary from the column default you can do that for the individual cells rather than looping through setting every one. I have to say I don't really understand the purpose of..
For i = 0 To (rowcnt / 2)
DtdgDataSource.Rows(i).DefaultCellStyle.BackColor = Color.LemonChiffon
Next
Why would you want to divide the DGV in half arbitrarily?
Thanks for reply
yes,that code fasten the execution a lot. We used the code simply for testing. But my problem is we are using same loops for flexgrid also,and its faster than DataGridView for same code. That i need to understand.Is there anything extra to do with DataGridView to speed up?
-
Jun 7th, 2018, 11:33 AM
#12
New Member
Re: Formatting is slow in DataGridView than MSFlexgrid
Originally Posted by winman
Thanks for reply
yes,that code fasten the execution a lot. We used the code simply for testing. But my problem is we are using same loops for flexgrid also,and its faster than DataGridView for same code. That i need to understand.Is there anything extra to do with DataGridView to speed up?
You must formating datagridview in zero rows result first.
For example :
Show data in 0 record result
Formating datagridview
Then show data you want to display.
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
|