下面是一個簡短的代碼示例,以便您可以測試控件的內存要求和性能。我沒有看到你應該做什麼來避免位圖,我認爲大多數第三方控件以類似的方式工作。我確信我的代碼可以通過多種方式進行優化,但您可以先從一些方面進行優化。不知道什麼時候想要在網格中有20000行,沒有用戶可以看到所有這些。也許人們可以想出一種方法來顯示子集...?
圖像的創建可能不應該在測試對象(因爲它是數據模型)中完成,而應該在表現層(我添加了DataBindingComplete事件,因爲這可以用於類似的事情),我做了在這裏,因爲它更容易。沒有圖像保存到文件或類似的東西。
我創建了一個名爲dataGridView1的DataGridView。
這是形式的代碼:
List<TestObject> _list = new List<TestObject>();
public Form1()
{
InitializeComponent();
dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
}
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
// Populate the grid, here you should add as many rows as you want to display
_list.Add(new TestObject("Obj1", 20, Brushes.Red, new int[]{3,4,5,3,5,6}));
_list.Add(new TestObject("Obj2", 10, Brushes.Green, new int[] { 1, 2, 3, 4, 5, 6 }));
_list.Add(new TestObject("Obj3", 30, Brushes.Blue, new int[] { 3, 2, 1, 1, 2, 3 }));
dataGridView1.DataSource = _list;
}
我還創建了一個測試對象來填充網格:
public class TestObject
{
private const int BitmapWidth = 100;
private const int BitmapHeight = 20;
private System.Drawing.Brush _color;
private string _name;
private int[] _numbers;
private int _value;
public TestObject(string name, int value, System.Drawing.Brush color, int[] series)
{
_name = name;
_numbers = series;
_color = color;
_value = value;
}
public string Name
{
get { return _name; }
}
public string Value { get { return _value.ToString(); } }
public Image Series
{
get
{
int width = BitmapWidth/_numbers.Length - _numbers.Length;
System.Drawing.Bitmap b = new Bitmap(BitmapWidth, BitmapHeight);
Graphics g = Graphics.FromImage(b);
g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
int current = 0;
for (int i = 0;i < _numbers.Length;i++)
{
g.FillRectangle(_color, current, BitmapHeight - (BitmapHeight/10) * _numbers[i], width, (BitmapHeight/10) * _numbers[i]);
current+=width + 2;
}
return b;
}
}
}
的WinForms,WPF或... – 2010-10-21 15:46:28
我想提出一個桌面應用程序即的WinForms – 2010-10-21 16:14:25
WPF,Silverlight和控制檯也是桌面應用程序。 – 2010-10-22 20:56:54