Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
I have a dataGridView1 which is of 100X100 size, cells are generated programmatically. When I'm running the program it is taking lot of time (5min) to display the data in the dataGrid. Could anyone please provide help to fix this problem?


C#
using System;
using System.Windows.Forms;


namespace MatrixToDataGridView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
   
            for (int k = 0; k < 100; k++)
            {
                dataGridView1.TopLeftHeaderCell.Value = "Theta / Phi";
                dataGridView1.Columns.Add("", k.ToString() );// dynamic cloumn adding
                dataGridView1.Rows.Add();//dynamic row adding
            }              
        }       
        public void function()
        {
            double [,] num = new double[100, 100];

            //Populates the array
            for (int i = 0; i < 100; i++)
            {
                for (int j = 0; j < 100; j++)
                {
                    num[i, j] = i+j;                     
                }
            }
            //Populates the dataGridView1
            for (int i = 0; i < 100; i++)
            {
                for (int j = 0; j < 100; j++)
                {
                    dataGridView1.Rows[i].Cells[j].Value = num[i, j].ToString();                    
                }
            }       
        }

        //Button for calling the function() to populate the array num and dataGridView1
        private void button1_Click(object sender, EventArgs e)
        {
            function(); 
        }
    }
}
Posted
Updated 25-Apr-14 12:23pm
v2
Comments
[no name] 25-Apr-14 18:48pm    
Well one thing that might help.... why are you populating an array and then populating a grid with the values from the array? Why don't you just populate the grid directly? You are not using the array for anything so creating it, populating it, then the grid from the values in the array is kind of useless.
Member 9794576 25-Apr-14 21:03pm    
Thank you very much for your suggestion. The problem solved when I made "AutoSizeColumnsMode" of datagridview1 in properties to "Displayed Cells" .

Your suggestion of directly populating datagridview1 is helpful.

It takes forever because you're populating each cell, individually. That's 10,000 calls to set each cell. The DataGridView is NOT Excel! You're trying to force the DGV to do something it was not designed to do.

On my machine, your code takes less than 1 second to run. What are you running this on?
 
Share this answer
 
Comments
Member 9794576 25-Apr-14 20:59pm    
Thank you for your reply. The problem solved when I made "AutoSizeColumnsMode" of datagridview1 in properties to "Displayed Cells" instead of "AllCells" .
The problem solved when I made "AutoSizeColumnsMode" of datagridview1 in properties to "Displayed Cells" .
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900