Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I have a DataGridView and i what to fill with 50000 record but it taken about 45s.
Is there a faster way.

In the form whare i have the datagridview i use this code to fill it:

C#
DataTable dataIN = new DataTable();
        public winContracteActive()
        {
            InitializeComponent();
            incarcaProduse();
        }

        private void winContracteActive_Load(object sender, EventArgs e)
        {

        }
        private void incarcaProduse()
        {
            try
            {
                AsyncProcessDelegate d = delegate()
                {
                    dataIN = InterogariDB.incarcaProduse();
                    this.Invoke((MethodInvoker)delegate
                    {
                        citireDate("contracteActive");
                    });
                };
                RunAsyncOperation(d);
            }
            catch (Exception Ex)
            {
                string msg = "Cod eroare: 001. \n\nEroare: \n" + Ex.Message;
                DoHandleException(msg);
            }
        }
        private void citireDate(string tipInterogare)
        {
            try
            {
                AsyncProcessDelegate d = delegate()
                {
                    ContractItem[] produseContract = InterogariDB.filtrareProduse(dataIN, tipInterogare);
                    this.Invoke((MethodInvoker)delegate
                    {
                        dContracte.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
                        dContracte.SuspendLayout();
                        try
                        {
                            dContracte.DataSource = produseContract;
                        }
                        finally
                        {
                            dContracte.ResumeLayout();
                        }
                    });
                };
                RunAsyncOperation(d);
            }
            catch (Exception Ex)
            {
                string msg = "Cod eroare: 001. \n\nEroare: \n" + Ex.Message;
                DoHandleException(msg);
            }
        }



To retrieve the data from database i have a class and use:

C#
internal static DataTable incarcaProduse()
        {
            Program.Connection.CommandText = "select ContractItems.ContractItemId, ContractItems.ContractId, ContractItems.Name, ContractItems.Description, ContractItems.Payment, ContractItems.Quantity, "
            + "ContractItems.QuantityUnit, IIF(ContractItems.DeletedId Is Null, 0, ContractItems.DeletedId) AS idStergere, IIF(ContractItems.GramajBrut Is Null, 0, ContractItems.GramajBrut) AS GramajBrut, " +
            " IIF(ContractItems.CategorieProdus Is Null, 0, ContractItems.CategorieProdus) AS CategorieProdus"
            + ", Contracts.CreateDate, Contracts.StartDate, Contracts.EndDate FROM Contracts INNER JOIN ContractItems ON Contracts.ContractId = ContractItems.ContractId";
            DataTable dataTI = new DataTable();
            Program.Connection.FillDataTable(dataTI, true);
            return dataTI;
        }
        internal static ContractItem[] filtrareProduse(DataTable dataIN, string tipInterogare)
        {
            DataTable dataFI = new DataTable();
            switch (tipInterogare)
            {
                case "contracteActive":
                    dataFI = dataIN.AsEnumerable()
                          .Where(i => i.Field<int>("idStergere") == 0)
                          .OrderBy(i => i.Field<int>("ContractId"))
                          .CopyToDataTable();
                    break;

                case "toateContractele":
                    dataFI = dataIN.AsEnumerable()
                          .Where(i => i.Field<int>("idStergere") != 0)
                          .OrderBy(i => i.Field<int>("ContractId"))
                          .CopyToDataTable();
                    break;

            }



            if (dataFI.Rows.Count == 0)
            {
                throw new Exception(string.Format("Nu sunt contracte in baza de date."));
            }
            return Lista<ContractItem>(dataFI, delegate(DataRow rezultatTB)
            {
                return new ContractItem(rezultatTB);
            });
        }
Posted
Updated 28-Sep-14 3:39am
v2
Comments
Richard MacCutchan 28-Sep-14 9:52am    
50,000 records! And how do you think your users are going to like searching through that lot? Please, learn how to present data properly by using proper selection criteria.
aciobanita constantin 28-Sep-14 10:04am    
I have selection criteria, maybe i will never need tho show all the records. just to be there to show all.
Richard MacCutchan 28-Sep-14 10:26am    
Well it's not a good idea. Firstly, as you have discovered it takes some time to load that number of records. And secondly, no user is going to accept an application that requires them to scroll through that many.

Yeah, there's a faster way. DON'T LOAD IT UP WITH 50,000 RECORDS!!

Honestly, who in their right mind is going to want to look at ALL Of those records?? NOBODY!!

Use paging and filtering techniques to help the user get the dataset down to what THEY want to see, NOT what YOU want to throw at them.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Sep-14 13:05pm    
Sure, a 5.
—SA
1.By default, in using the GridView control for manipulating a large amount of data (a few hundreds of rows), the performance is very low, and if the number of rows increases to several hundreds or several thousands, the web page becomes useless. Note that there is the loading time from DB, then the grid data will be cached in the page and sent between web server and browser on each postback.

2.So the solution is to use pagination. You can find details and source code example in my next article: Advanced ASPX GridView Pagination and Data Entities[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Sep-14 13:06pm    
5ed.
—SA
Raul Iloc 28-Sep-14 13:14pm    
Thank for your vote!

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