ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码
当前位置:首页 >> 低调看直播体育app软件下载 >> .NETjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播 >> 利用winform中DataGridView的显示较大数量的的数据

利用winform中DataGridView的显示较大数量的的数据

来源:网络整理     时间:2015-03-20     关键词:

本篇文章主要介绍了"利用winform中DataGridView的显示较大数量的的数据",主要涉及到方面的内容,对于.NETjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下: 在大数据量的情况下,想要通过DataGridView直接显示出来.明显不是一个很好的办法,甚至有可能用在可视化界面的资源高于需要显示的数据很多.一种可行的方案就...

在大数据量的情况下,想要通过DataGridView直接显示出来.明显不是一个很好的办法,甚至有可能用在可视化界面的资源高于需要显示的数据很多.

一种可行的方案就是使用虚表,虚表的工作原理也比较简单,概括起来就是动态的将数据加载
到一个行数很少的DataGridview中.

如果只是想展示数据而不需要修改数据,只需要将DataGridview的VirtualMode设置为True,添加好列对象,然后添加CellValueNeeded委托就行了,如下:

this.dataGridView1.CellValueNeeded += new DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);

通过这个委托的名字大致就可以知道,应该是需要在这里为某个需要数据的Cell填充数据.这个数据的来源应该是后台的数据源.同时,通过事件的参数中可以得到需要加载数据的行和列的下标,在这里只需要将需要提交的数据赋值给e.Value就行了.(需要注意的是:在初始化的时候应该为DataGridView.RowCount设置一个值,这个值是需要显示的数据的列的总数.同时尽量不要在加载数据的委托里边向控制台输出信息或者其他耗时的操作)

到这里,如果只是需要简单的显示数据而不用编辑数据,一个方便快捷的虚表就完成了.但是如果数据量超过一定的限度,这个标的加载速度比较慢.同时根据使用情况来看,在初始化的时候会全部加载数据,然后在显示出来的时候又会全部加载一次数据,鼠标扫过一行也会调用一次数据加载的委托.这个没有怎么去深究过.

总结:使用起来比较方便快捷,不过这个主要是为可编辑的大数据设计的.因此如果仅仅只是想展示大数据量的话,这个解决方案还不是很好,

下面是微软MSDN的示例源码和链接:

using System.IO;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System;

public class VirtualModeDemo : Form
{
    DataGridView dataGridView1 = new DataGridView();

    public VirtualModeDemo()
        : base()
    {
        Text = "DataGridView virtual-mode demo (cell-level commit scope)";
        dataGridView1.NewRowNeeded +=
            new DataGridViewRowEventHandler(dataGridView1_NewRowNeeded);
        dataGridView1.RowsAdded +=
            new DataGridViewRowsAddedEventHandler(dataGridView1_RowsAdded);
        dataGridView1.CellValidating +=
            new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);
        dataGridView1.CellValueNeeded +=
            new DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);
        dataGridView1.CellValuePushed +=
            new DataGridViewCellValueEventHandler(dataGridView1_CellValuePushed);

        Controls.Add(dataGridView1);
        dataGridView1.VirtualMode = true;
        dataGridView1.AllowUserToDeleteRows = false;
        dataGridView1.Columns.Add("Numbers", "Positive Numbers");
        dataGridView1.Rows.AddCopies(0, initialSize);
    }

    bool newRowNeeded;
    private void dataGridView1_NewRowNeeded(object sender,
        DataGridViewRowEventArgs e)
    {
        newRowNeeded = true;
    }

    const int initialSize = 5000000;
    int numberOfRows = initialSize;

    private void dataGridView1_RowsAdded(object sender,
         DataGridViewRowsAddedEventArgs e)
    {
        if (newRowNeeded)
        {
            newRowNeeded = false;
            numberOfRows = numberOfRows + 1;
        }
    }

    #region "data store maintance"
    const int initialValue = -1;

    private void dataGridView1_CellValueNeeded(object sender,
        DataGridViewCellValueEventArgs e)
    {
        if (store.ContainsKey(e.RowIndex))
        {
            // Use the store if the e value has been modified 
            // and stored.            
            e.Value = store[e.RowIndex];
        }
        else if (newRowNeeded && e.RowIndex == numberOfRows)
        {
            if (dataGridView1.IsCurrentCellInEditMode)
            {
                e.Value = initialValue;
            }
            else
            {
                // Show a blank value if the cursor is just resting
                // on the last row.
                e.Value = String.Empty;
            }
        }
        else
        {
            e.Value = e.RowIndex;
        }
    }

    private void dataGridView1_CellValuePushed(object sender,
        DataGridViewCellValueEventArgs e)
    {
        store.Add(e.RowIndex, int.Parse(e.Value.ToString()));
    }
    #endregion

    private Dictionary store = new Dictionary();

    private void dataGridView1_CellValidating(object sender,
        DataGridViewCellValidatingEventArgs e)
    {
        dataGridView1.Rows[e.RowIndex].ErrorText = "";
        int newInteger;

        // Don't try to validate the 'new row' until finished 
        // editing since there
        // is not any point in validating its initial value.
        if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; }
        if (!int.TryParse(e.FormattedValue.ToString(),
            out newInteger) || newInteger < 0)
        {
            e.Cancel = true;
            dataGridView1.Rows[e.RowIndex].ErrorText = "the value must be a non-negative integer";
        }
    }

    [STAThreadAttribute()]
    public static void Main()
    {
        Application.Run(new VirtualModeDemo());
    }
}

微软MSDN示例源码

以上就介绍了利用winform中DataGridView的显示较大数量的的数据,包括了方面的内容,希望对.NETjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播有兴趣的朋友有所帮助。

本文网址链接:http://www.codes51.com/article/detail_120247.html

相关图片

相关文章