Results 1 to 16 of 16

Thread: [2008] Performance in DatagridView

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    [2008] Performance in DatagridView

    Hi to all:

    I had notice that datagridview have a problem with performance,and i had read in MSDN something about the problem but sincerely this code that i use for fill a datagridview (and the DB I have have just 10 records,and I have problems with performance),i don't know if something in the code block the performance.I Would like to know yours suggestions for increase the performance in my code please.

    Code:
    Private Sub abrirBD()
    
    
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = " & My.Application.Info.DirectoryPath & "\NumWin.mdb"
            con.Open()
    
            ds = New DataSet
    
            sql = Nothing
    
            sql = _
            "select" & _
            " nemp, contribuinte, nome " & _
            " from Empresas" & _
            " order by nemp "
    
            da = New OleDb.OleDbDataAdapter(sql, con)
    
            da.Fill(ds, "table")
    
            DataGridView1.DataSource = ds.Tables("Table")
    
            DataGridView1.RowHeadersWidth = 4
    
    
             DataGridView1.SelectionMode =    DataGridViewSelectionMode.FullRowSelect
    
            DataGridView1.Columns(0).HeaderText = "NºEmpresa"
            DataGridView1.Columns(1).HeaderText = "Contribuinte"
            DataGridView1.Columns(2).HeaderText = "Nome Empresa"
    
           
            DataGridView1.ColumnHeadersHeight = 30
    
            
            DataGridView1.Columns(0).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
            DataGridView1.Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
            DataGridView1.Columns(2).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
    
            
            DataGridView1.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            DataGridView1.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    
    
            
            DataGridView1.DefaultCellStyle.SelectionBackColor = Color.Purple '(255; 192; 255)
    
            
            DataGridView1.Columns(0).Width = 70
            DataGridView1.Columns(1).Width = 80
            DataGridView1.Columns(2).Width = 360
    
    
    
            con.Close()
    
    
        End Sub
    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Performance in DatagridView

    The problem is most likely because you're first loading all the data and then you're making all those individual changes. The grid will be redrawn after each change and, if it's full of data, that can take quite a while.

    Instead of relying on the grid to create the columns for you, you should be creating them yourself. Add the columns in the designer and configure them exactly as you want with colours, widths, titles, etc. When you then bind the data the grid will only need to draw itself once.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    Re: [2008] Performance in DatagridView

    Hi:

    Thanks for your answear...
    I will do that you said and see if I have an increase of the performance...

    Thanks

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    Re: [2008] Performance in DatagridView

    Hi:

    Now I remember Why i had put code to setting datagridview...That's because when I create the fields in the design mode,the DGV add more 3 fields that correspond to the data in the code...
    For better understand:

    NºEmpresa Contribuinte Nome Empresa nemp contribuinte nome
    -------------------------------------- 01 15252525 Manuel ddddd
    --------------------------------------- 02 5263522 Oliveira
    --------------------------------------- 03 8965252 José

    Do you know the way to resolve this?

    Thanks

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Performance in DatagridView

    That would be because you haven't set the DataPropertyName of the columns you created. If you don't tell the grid to bind its existing columns to the corresponding columns in the data source then it won't. It will create new columns. You set the DataPropertyName of each column in the grid so it knows which column in the data source it needs to be bound to.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    Re: [2008] Performance in DatagridView

    Yes...you are right...I had set that property and now work well,but the performance stay the same...I don't see any increase of performance excluding code and setting by Design Mode...

    Any more suggestion?Or we don't have notthing more to do!

  7. #7

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    Re: [2008] Performance in DatagridView

    Yes off course:

    Code:
    Private Sub abrirBD()
    
    
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = " & My.Application.Info.DirectoryPath & "\NumWin.mdb"
            con.Open()
    
            ds = New DataSet
    
            sql = Nothing
    
            sql = _
            "select" & _
            " nemp, contribuinte, nome " & _
            " from Empresas" & _
            " order by nemp "
    
            da = New OleDb.OleDbDataAdapter(sql, con)
    
            da.Fill(ds, "table")
    
            DataGridView1.DataSource = ds.Tables("Table")
       
            DataGridView1.RowHeadersWidth = 4
    
            con.Close()
    
    
        End Sub

  9. #9
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: [2008] Performance in DatagridView

    Is is necessary to open the connection in abrirBD() procedure? Cannot you open it on form load or something. You are facing this performance issue all because of this. Connection is the culprit who is eating all the performance.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    Re: [2008] Performance in DatagridView

    Hi:

    I had put,the code, in the form load but the problem remain...

  11. #11

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    Re: [2008] Performance in DatagridView

    code:

    Code:
    Private Sub Empresas_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            varentrada = 1
    
    con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = " & My.Application.Info.DirectoryPath & "\NumWin.mdb"
            con.Open()
    
            ds = New DataSet
    
            sql = Nothing
    
            sql = _
            "select" & _
            " nemp, contribuinte, nome " & _
            " from Empresas" & _
            " order by nemp "
    
            da = New OleDb.OleDbDataAdapter(sql, con)
    
            da.Fill(ds, "table")
    
            DataGridView1.DataSource = ds.Tables("Table")
       
            DataGridView1.RowHeadersWidth = 4
    
            con.Close()
    
    
            'abrirBD()
            
        End Sub

  13. #13
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: [2008] Performance in DatagridView

    What I meant is something different, anyways lave it. Establishing a connection with database is a time consuming task. It will take its own time for it and you cannot have control over it. Rest is fine with your code, I don't see any thing for further improvements.

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    Re: [2008] Performance in DatagridView

    In fact is strange,because i have in this DB only 10 records and the DGV is fill in "slow mode",and I have a good PC with good memory,and the OS is XP..
    Imaging when The DB have 1000 records...I have to wait 1 minute to fill the DGV???
    This control is very heavy!!!
    Solutions:change to Listview?perhaps!!!

  15. #15
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: [2008] Performance in DatagridView

    Do one thing... Write he connection opening code in form load event and write rest of code which loads the data in datagridview on button's click event. Click the button and see how much time it takes to load the data. That is the actual time it's taking to load data and rest of the time is consumed by connection.

  16. #16

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    Re: [2008] Performance in DatagridView

    Hi:
    Sory the delay of the answear...
    The time consumed it's all to load data...
    How it's possible????

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