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
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.
Re: Formatting is slow in DataGridView than MSFlexgrid
Quote:
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
Re: Formatting is slow in DataGridView than MSFlexgrid
Quote:
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
Re: Formatting is slow in DataGridView than MSFlexgrid
Quote:
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
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
Re: Formatting is slow in DataGridView than MSFlexgrid
Quote:
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
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.
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?
Re: Formatting is slow in DataGridView than MSFlexgrid
Quote:
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
Re: Formatting is slow in DataGridView than MSFlexgrid
Quote:
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?
Re: Formatting is slow in DataGridView than MSFlexgrid
Quote:
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.