Click here to Skip to main content
16,015,504 members
Home / Discussions / C#
   

C#

 
GeneralRe: Speeding up DataGridView RowCount assignment Pin
Wendelius27-Oct-08 10:49
mentorWendelius27-Oct-08 10:49 
GeneralRe: Speeding up DataGridView RowCount assignment Pin
AndrusM27-Oct-08 23:53
AndrusM27-Oct-08 23:53 
GeneralRe: Speeding up DataGridView RowCount assignment Pin
Luc Pattyn27-Oct-08 10:33
sitebuilderLuc Pattyn27-Oct-08 10:33 
GeneralRe: Speeding up DataGridView RowCount assignment Pin
AndrusM27-Oct-08 22:58
AndrusM27-Oct-08 22:58 
GeneralRe: Speeding up DataGridView RowCount assignment Pin
Luc Pattyn27-Oct-08 23:28
sitebuilderLuc Pattyn27-Oct-08 23:28 
GeneralRe: Speeding up DataGridView RowCount assignment Pin
AndrusM28-Oct-08 0:09
AndrusM28-Oct-08 0:09 
GeneralRe: Speeding up DataGridView RowCount assignment Pin
Luc Pattyn28-Oct-08 0:31
sitebuilderLuc Pattyn28-Oct-08 0:31 
GeneralRe: Speeding up DataGridView RowCount assignment Pin
AndrusM28-Oct-08 3:30
AndrusM28-Oct-08 3:30 
Luc,

2) for unknown reasons RowCount-first is faster

Thank you very much. You are genious.
I tried this in my applicaton and this eliminates RowCount delay.

Now there is only 3 sec delay which is caused by reading first two pages to cache like in MSDN sample.
There is also SELECT COUNT(*) FROM mytable command but this seems not to cause any delay.

the DGV seems to create objects for all the cells right from
the start, and creating 108 million objects is bound to take a couple


108 millions of objects should take visible in Task Manager amout of memory. Task Manager memory (working set) does not show significat memory consumption.
So I think that 108 million of objects are not created.

Maybe this is related to sililar issue which posted here and in MS product feedback and which does not have any solution:

Steps to reproduce:

1. Run code below
2. Double click in grid header in separation line between columns 1 ja 2

Observed:

CPU usage goes to 100% for a long time.

using System;<br />
using System.Windows.Forms;<br />
using System.Collections.Generic;<br />
<br />
class test {<br />
[STAThreadAttribute()]<br />
public static void Main() {<br />
Application.Run(new VirtualModeForm());<br />
}<br />
}<br />
<br />
class VirtualModeForm : Form {<br />
private List<DataObject> m_Data = new List<DataObject>();<br />
private List<bool> m_Visited = new List<bool>();<br />
DataGridView m_Grid = new DataGridView();<br />
public VirtualModeForm() {<br />
Controls.Add(m_Grid);<br />
m_Grid.CellValueNeeded += OnCellValueNeeded;<br />
InitData();<br />
InitGrid();<br />
}<br />
<br />
private void InitData() {<br />
for (int i = 0; i < 1000001 + 1; i++) {<br />
m_Visited.Add(false);<br />
DataObject obj = new DataObject();<br />
obj.Id = i;<br />
obj.Val = 2 * i;<br />
m_Data.Add(obj);<br />
} }<br />
<br />
private void InitGrid() {<br />
m_Grid.Dock = DockStyle.Fill;<br />
m_Grid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCellsExceptHeader);<br />
m_Grid.VirtualMode = true;<br />
m_Grid.ReadOnly = true;<br />
m_Grid.ColumnCount = 3;<br />
m_Grid.Rows.Add();<br />
m_Grid.Rows.AddCopies(0, 1000000);<br />
}<br />
<br />
private void OnCellValueNeeded(object sender, DataGridViewCellValueEventArgs e) {<br />
<br />
m_Visited[e.RowIndex] = true;<br />
if (e.ColumnIndex == 0) {<br />
  e.Value = m_Data[e.RowIndex].Id;<br />
} else if (e.ColumnIndex == 1) {<br />
  e.Value = m_Data[e.RowIndex].Val;<br />
 } else if (e.ColumnIndex == 2) {<br />
<br />
Random rand = new Random();<br />
e.Value = rand.Next();<br />
}<br />
}<br />
}<br />
<br />
public class DataObject {<br />
private int m_Id;<br />
private int m_Val;<br />
<br />
public int Val {<br />
get { return m_Val; }<br />
set { m_Val = value; }<br />
}<br />
<br />
public int Id {<br />
get { return m_Id; }<br />
set { m_Id = value; }<br />
}<br />
}


How to fix this ?

BTW: you should not add the grid to Controls before it has been set up.

I tried it but this does not to affect to speed at all.
My base class is created using Winforms designer which adds grid in form constructur. If I modify this code manually changes are lost when designer re-creates code.
Code sample of this is below.

Questions:

1. Why I should not add grid before setting ?
2. How to add grid only after it has set without modifying designer generated grid code manually to code below? I need to use subclassed form because main form contains generic type parameter and Winforms designer does not allow to design form with generic.

using System;<br />
using System.Windows.Forms;<br />
<br />
class Test<br />
{<br />
    public static void Main()<br />
    {<br />
        Application.Run(new VirtualModeForm<Customer>());<br />
    }<br />
}<br />
<br />
class VirtualModeForm<T> : BaseForm<br />
{<br />
    public VirtualModeForm()<br />
    {<br />
        grid.VirtualMode = true;<br />
        grid.RowCount = 1000000;<br />
        grid.ColumnCount = 108;<br />
    }<br />
}<br />
<br />
class Customer { }<br />
<br />
// code below is generated by WinForms designer<br />
class BaseForm : Form<br />
{<br />
    internal BaseForm()<br />
    {<br />
        InitializeComponent();<br />
    }<br />
<br />
    void InitializeComponent()<br />
    {<br />
        grid = new DataGridView();<br />
        Controls.Add(grid);<br />
    }<br />
    protected DataGridView grid;<br />
}


Andrus

Questionhow to make call to telephone in c# Pin
ramu20u27-Oct-08 9:21
ramu20u27-Oct-08 9:21 
AnswerRe: how to make call to telephone in c# Pin
MidwestLimey27-Oct-08 9:38
professionalMidwestLimey27-Oct-08 9:38 
AnswerRe: how to make call to telephone in c# Pin
PIEBALDconsult27-Oct-08 18:20
mvePIEBALDconsult27-Oct-08 18:20 
Questionproblem with creating a messagequeue Pin
prasadbuddhika27-Oct-08 9:10
prasadbuddhika27-Oct-08 9:10 
AnswerRe: problem with creating a messagequeue Pin
manfred.bjorlin27-Oct-08 9:14
manfred.bjorlin27-Oct-08 9:14 
QuestionProblems with deserialization of MessageQueue Message Pin
manfred.bjorlin27-Oct-08 9:04
manfred.bjorlin27-Oct-08 9:04 
AnswerRe: Problems with deserialization of MessageQueue Message Pin
Le centriste27-Oct-08 9:33
Le centriste27-Oct-08 9:33 
GeneralRe: Problems with deserialization of MessageQueue Message Pin
manfred.bjorlin28-Oct-08 0:01
manfred.bjorlin28-Oct-08 0:01 
GeneralRe: Problems with deserialization of MessageQueue Message Pin
Le centriste28-Oct-08 2:19
Le centriste28-Oct-08 2:19 
GeneralRe: Problems with deserialization of MessageQueue Message Pin
manfred.bjorlin28-Oct-08 2:29
manfred.bjorlin28-Oct-08 2:29 
QuestionBuilding a FAT emulator Pin
Gianpaolo Barci27-Oct-08 7:23
Gianpaolo Barci27-Oct-08 7:23 
AnswerRe: Building a FAT emulator Pin
Michael Bookatz27-Oct-08 7:32
Michael Bookatz27-Oct-08 7:32 
GeneralRe: Building a FAT emulator Pin
Gianpaolo Barci27-Oct-08 7:48
Gianpaolo Barci27-Oct-08 7:48 
GeneralRe: Building a FAT emulator Pin
User 665827-Oct-08 11:33
User 665827-Oct-08 11:33 
QuestionDeleting Items and Selecting next one on ListBox on C# Pin
three.leaf27-Oct-08 6:33
three.leaf27-Oct-08 6:33 
AnswerRe: Deleting Items and Selecting next one on ListBox on C# Pin
Parwej Ahamad27-Oct-08 7:25
professionalParwej Ahamad27-Oct-08 7:25 
AnswerRe: Deleting Items and Selecting next one on ListBox on C# Pin
Michael Bookatz27-Oct-08 7:30
Michael Bookatz27-Oct-08 7:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.