Results 1 to 12 of 12

Thread: Formatting is slow in DataGridView than MSFlexgrid

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    248

    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

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    248

    Re: Formatting is slow in DataGridView than MSFlexgrid

    Quote Originally Posted by dunfiddlin View Post
    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

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    248

    Re: Formatting is slow in DataGridView than MSFlexgrid

    Quote Originally Posted by dunfiddlin View Post
    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

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    248

    Re: Formatting is slow in DataGridView than MSFlexgrid

    Quote Originally Posted by Pradeep1210 View Post
    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

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    248

    Re: Formatting is slow in DataGridView than MSFlexgrid

    Quote Originally Posted by Pradeep1210 View Post
    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.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    248

    Re: Formatting is slow in DataGridView than MSFlexgrid

    Quote Originally Posted by dunfiddlin View Post
    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?

  12. #12
    New Member
    Join Date
    Jun 2018
    Posts
    1

    Re: Formatting is slow in DataGridView than MSFlexgrid

    Quote Originally Posted by winman View Post
    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
  •  



Click Here to Expand Forum to Full Width