|
-
Sep 24th, 2008, 11:00 AM
#1
Thread Starter
Frenzied Member
[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
-
Sep 24th, 2008, 08:21 PM
#2
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.
-
Sep 25th, 2008, 03:23 AM
#3
Thread Starter
Frenzied Member
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
-
Sep 25th, 2008, 03:44 AM
#4
Thread Starter
Frenzied Member
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
-
Sep 25th, 2008, 03:51 AM
#5
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.
-
Sep 25th, 2008, 03:58 AM
#6
Thread Starter
Frenzied Member
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!
-
Sep 25th, 2008, 04:13 AM
#7
Re: [2008] Performance in DatagridView
Can you please post your new modified code.
-
Sep 25th, 2008, 04:16 AM
#8
Thread Starter
Frenzied Member
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
-
Sep 25th, 2008, 04:21 AM
#9
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.
-
Sep 25th, 2008, 04:29 AM
#10
Thread Starter
Frenzied Member
Re: [2008] Performance in DatagridView
Hi:
I had put,the code, in the form load but the problem remain...
-
Sep 25th, 2008, 04:33 AM
#11
Re: [2008] Performance in DatagridView
Please post the new code again just see that every thing is as expected.
-
Sep 25th, 2008, 04:35 AM
#12
Thread Starter
Frenzied Member
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
-
Sep 25th, 2008, 04:46 AM
#13
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.
-
Sep 25th, 2008, 04:51 AM
#14
Thread Starter
Frenzied Member
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!!!
-
Sep 25th, 2008, 05:22 AM
#15
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.
-
Sep 25th, 2008, 06:33 AM
#16
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|