您的位置:首页 > 其它

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

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

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

到一个行数很少的DataGridview中.

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

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

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

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

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

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

[csharp] view
plaincopy

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<int, int> store = new Dictionary<int, int>();

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示例源码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: