更快地填充DataGridView的方法 [英] Faster way to fill DataGridView

查看:72
本文介绍了更快地填充DataGridView的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一个DataGridView,我要用50000记录填充什么,但需要大约45秒。

有没有更快的方式。



在形式whare我有datagridview我用这个代码填写它:



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

private void winContracteActive_Load( object sender,EventArgs e)
{

}
private void incarcaProduse()
{
try
{
AsyncProcessDelegate d = 委托()
{
dataIN = InterogariDB.incarcaProduse();
this .Invoke((MethodInvoker) delegate
{
citireDate ( contracteActive);
});
};
RunAsyncOperation(d);
}
catch (例外情况)
{
string msg = Cod eroare:001. \ n \ nEroare:\ n + Ex.Message;
DoHandleException(msg);
}
}
私有 void citireDate( string tipInterogare)
{
try
{
AsyncProcessDelegate d = < span class =code-keyword> delegate ()
{
ContractItem [] produseContract = InterogariDB.filtrareProduse(dataIN,tipInterogare);
this .Invoke((MethodInvoker) delegate
{
dContracte .DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dContracte.SuspendLayout();
尝试
{
dContracte.DataSource = produseContract;
}
finally
{
dContracte.ResumeLayout();
}
});
};
RunAsyncOperation(d);
}
catch (例外情况)
{
string msg = Cod eroare:001. \ n \ nEroare:\ n + Ex.Message;
DoHandleException(msg);
}
}







要从数据库中检索数据我有一个上课和使用:



<前lang =cs> 内部 static DataTable incarcaProduse()
{
Program.Connection.CommandText = 选择ContractItems.ContractItemId,ContractItems.ContractId,ContractItems.Name,ContractItems.Description,ContractItems.Payment,ContractItems.Quantity,
+ ContractItems.QuantityUnit,IIF(ContractItems.DeletedId为Null,0,ContractItems.DeletedId)AS idStergere,IIF(ContractItems.GramajBrut为Null,0,ContractItems.GramajBrut)AS GramajBrut, +
IIF(ContractItems.CategorieProdus为Null,0,ContractItems.Categor ieProdus)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;
}
内部 静态 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 异常( string .Format( Nu sunt在baza de date收缩。));
}
返回 Lista< ContractItem>(dataFI,委托(DataRow rezultatTB)
{
return new ContractItem(rezultatTB);
}) ;
}

解决方案

是的,有更快的方法。请不要加载50,000张记录!!



老实说,谁心不在想要查看所有这些记录? NOBODY !!



使用分页和过滤技术帮助用户将数据集下载到他们想要看到的内容,而不是你想要的东西。


1.默认情况下,在使用GridView控件处理大量数据(几百行)时,性能非常低,如果行数增加到几百行或几千,网页变得无用。请注意,有来自DB的加载时间,然后网格数据将缓存在页面中,并在每次回发时在Web服务器和浏览器之间发送。



2.So解决方案是使用分页。您可以在我的下一篇文章中找到详细信息和源代码示例:高级ASPX GridView分页和数据实体 [ ^ ]


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:

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:

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);
            });
        }

解决方案

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.


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[^]


这篇关于更快地填充DataGridView的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆