本文整理汇总了C#中System.Windows.Forms.DataGridView.VirtualMode属性的典型用法代码示例。如果您正苦于以下问题:C# DataGridView.VirtualMode属性的具体用法?C# DataGridView.VirtualMode怎么用?C# DataGridView.VirtualMode使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Windows.Forms.DataGridView
的用法示例。
在下文中一共展示了DataGridView.VirtualMode属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DataGridView
//引入命名空间
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());
}
}