使用DataGridView显示具有过滤能力的大文件 [英] Using DataGridView for displaying large files with filter ability

查看:82
本文介绍了使用DataGridView显示具有过滤能力的大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要根据一个或多个列查看具有过滤能力的大(> 10 MB,~100000行)制表符分隔文本文件。 (我不需要任何索引或编辑功能)

Hi,

I need to view large ( > 10 MB, ~100000 lines) tab delimitered text  files with filtering ability according to one or more of the columns. (I do not need any sording or editing abilities)

我想到的解决方案是将delimetered文件加载到数据表中并在DataGridView控件中显示它。

The solution I thought about is loading the delimetered file into a data table and showing it in a DataGridView control. 

为了获得更好的性能,我使用了http://msdn2.microsoft.com/en-us/library/ms171624.aspx 使用表格I作为数据源从文件而不是数据库连接创建。

For achieving better performance I used an caching mechanism described in http://msdn2.microsoft.com/en-us/library/ms171624.aspx using as datasource the table I created from the file instead of database connection.

尝试过滤数据时我正在使用 table.Select(filter)并使用DataRetriever返回的行class(根据上述文章)更新缓存机制。

When trying to filter the data I'm using table.Select(filter) and using the returned rows with DataRetriever class  (according to the abovementioned article) to update the caching mechanism.

之后我通过 这个清除DataGridView <强> .dgridView.Rows.Clear(); 并通过 .dgridView.RowCount = retriever.RowCount来支持RowCount的属性;

Afterwards I'm clearing the DataGridView by this.dgridView.Rows.Clear(); and asigning the RowCount proprerty by this.dgridView.RowCount = retriever.RowCount;

第一个赋值(所有行)需要一段合理的时间,但是当使用过滤器放大当前显示最后一个赋值的行数时(RowCount) )需要很长一段时间。

The first assignment (all of the rows) takes an a reasonable period of time but when using a filter that enlarges the amount of rows that currently displayed the last assignment (RowCount) takes enormous period of time.

除此之外我注意到 CellValueNeeded CellFormatting 事件的火灾频率与数量不成比例当前显示的单元格,如果出现性能原因可能是一个。

Except this I noticed that the CellValueNeeded and CellFormatting events fire frequency is unproportional to the number of cells currently displayed and that may be one if the reasons for performance proplem.

我的问题是:

1。它是我能用于满足我需求的最佳控件吗?

1. Is it the best control I can use for my needs ?

2。这是我正确使用DataGridView控件的正确方法吗?

2. Is this a right way I'm using the DataGridView contol for my needs ?

2。有什么方法可以改善性能,目前的性能是不可接受的吗?

2. Is there any way I can improve the performance, current performance is unacceptable ?

谢谢,

推荐答案

你好,我改变了一些外部过滤器的代码所以我添加这些代码




 



  
hello , i changed little bit this code for external filter so i add these code

 

  
 private void LoadSqlData(ref DataGridView dgv,string sfilter)
        {


            try
            {

                dgv.CellValueNeeded -= new DataGridViewCellValueEventHandler(dgv_CellValueNeeded);

             
               
                dgv.RowCount = 0;
                dgv.Columns.Clear();



                 DataRetriever retriever =
                     new  DataRetriever(project.Properties.Settings.Default.ConnectionString, "Artikelen", sfilter );  //for example "bWebEnabled=0"
                memoryCache = new  Cache(retriever, 50);
                foreach (DataColumn column in retriever.Columns)
                {
                    dgv.Columns.Add(
                        column.ColumnName, column.ColumnName);
                }
                    dgv.RowCount = retriever.RowCount;
            }
            catch (SqlException se)
            {

             //



            }


          }




$


&NBSP;&NBSP;&NBSP; public DataTable SupplyPageOfData(int lowerPageBoundary,int rowsPerPage)

        {

            //存储ID列的名称。此列必须包含唯一的


            //值,以便下面的SQL能正常工作。

            if(columnToSortBy == null)

            {

                columnToSortBy = this.Columns [0] .ColumnName;

            }


            if(!this.Columns [columnToSortBy] .Unique)

            {

               抛出新的InvalidOperationException(String.Format(&
                    "列{0}必须包含唯一值。",columnToSortBy));

           &NBSP; }


            //从数据库中检索指定的行数,开始为
            //使用lowerPageBoundary参数指定的行。

            command.CommandText =" Select Top" + rowsPerPage +" " +

                CommaSeparatedListOfColumnNames +"来自" + tableName +

                "在哪里" + columnToSortBy +" NOT IN(选择顶部                lowerPageBoundary +" " + columnToSortBy +" From" +

                tableName +" Order By" + columnToSortBy +

               " )" +(this.sfilter!= string.Empty?" AND" + sfilter +"" ;: string.Empty)   +"排序依据" + columnToSortBy;

            adapter.SelectCommand = command;



            DataTable table = new DataTable();

            table.Locale = System.Globalization.CultureInfo.InvariantCulture;

            adapter.Fill(table);

           退货表;

        } b




直到这里工作一切正常你可以看到我将sfilter添加到sql语句。



但是在过滤语句发生后我在Cache类中遇到错误。





          }





    public DataTable SupplyPageOfData(int lowerPageBoundary, int rowsPerPage)
        {
            // Store the name of the ID column. This column must contain unique
            // values so the SQL below will work properly.
            if (columnToSortBy == null)
            {
                columnToSortBy = this.Columns[0].ColumnName;
            }

            if (!this.Columns[columnToSortBy].Unique)
            {
                throw new InvalidOperationException(String.Format(
                    "Column {0} must contain unique values.", columnToSortBy));
            }

            // Retrieve the specified number of rows from the database, starting
            // with the row specified by the lowerPageBoundary parameter.
            command.CommandText = "Select Top " + rowsPerPage + " " +
                CommaSeparatedListOfColumnNames + " From " + tableName +
                " WHERE " + columnToSortBy + " NOT IN (SELECT TOP " +
                lowerPageBoundary + " " + columnToSortBy + " From " +
                tableName + " Order By " + columnToSortBy +
                ") " + (this.sfilter != string.Empty? " AND "+sfilter +" ": string.Empty)   + "Order By " + columnToSortBy;
            adapter.SelectCommand = command;

            DataTable table = new DataTable();
            table.Locale = System.Globalization.CultureInfo.InvariantCulture;
            adapter.Fill(table);
            return table;
        }


until here works everything ok like you can see i added sfilter to sql statement.

but after filter statement happend i got error here in Cache class.


private bool IfPageCached_ThenSetElement(int rowIndex,
            int columnIndex, ref string element)
        {
            if (IsRowCachedInPage(0, rowIndex))
            {
                element = cachePages[0].table
                    .Rows[rowIndex % RowsPerPage][columnIndex].ToString();
//here comes error
                return true;
            }
            else if (IsRowCachedInPage(1, rowIndex))
            {
                element = cachePages[1].table
                    .Rows[rowIndex % RowsPerPage][columnIndex].ToString();
                return true;
            }

            return false;
        }




System.Data.dll中出现类型为"System.IndexOutOfRangeException"的未处理异常

¥ b $ b附加信息:位置9没有排。

b
预付款由于
djramc


An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll

Additional information: There is no row at position 9.

thanks by advance
djramc


这篇关于使用DataGridView显示具有过滤能力的大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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