首页 > 解决方案 > 在虚拟模式下,我的datagridview在多次加载时出现渣滓

问题描述

我正在使用带缓存的虚拟模式在我的 WinForms 应用程序中加载我的 datagridview,使用的代码与此处相同

更改了SupplyData功能以能够搜索如下数据

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.
        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 CPNum, strftime('%d-%m-%Y', CPEdtDate) AS CPEdtDate From " + tableName + " WHERE CPNum LIKE '%" + search + "%' AND " + columnToSortBy + " NOT IN (SELECT " + columnToSortBy + " From " +
            tableName + " WHERE CPNum LIKE '%" + search + "%' Order By " + columnToSortBy + " LIMIT " + lowerPageBoundary + ") Order By " + columnToSortBy + " LIMIT " + rowsPerPage;
        adapter.SelectCommand = command;

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

重新加载网格时,我使用以下代码

DataRetriever retriever =
            new DataRetriever(connectionString, table, search);
        memoryCache = new Cache(retriever, 16);
        foreach (DataColumn column in retriever.Columns)
        {
            if (!masterChartGrid.Columns.Contains(column.ColumnName))
            {
                masterChartGrid.Columns.Add(
                        column.ColumnName, "Edition Date");
            }
        }
        masterChartGrid.Rows.Clear();
        this.masterChartGrid.RowCount = retriever.RowCount;

它适用于搜索数据。但是当我再次尝试重置搜索和重新加载表时,应用程序变得无响应。

标签: c#.netwinformsdatagridviewvirtual

解决方案


当我从.cs文件中以编程方式添加 DataGridView 时,它起作用了。

private DataGridView masterChartGrid = new DataGridView();


推荐阅读