Here is a short code example, just so you can check the memory requirements and performance of your controls. I don’t see what you should do to avoid bitmaps; I think most third-party controls work in a similar way. I'm sure my code can be optimized in several ways, but you have a place to start. Not sure when it would be necessary to have 20,000 rows in a grid, not a single user can see all this. Perhaps you can figure out a way to display the subsets at the same time.?
The image creation probably should not be done in the test object (as in the data model), but in the presentation layer (I added the DataBindingComplete event, since it can be used for such things), I did it here because it was easier. Images are not saved in a file or something like that.
I created a form with a DataGridView called dataGridView1.
This is the form code:
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 ) {
I also created a test object to fill the grid:
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; } } }
Mikael
source share