Hi,
I want to display an icon in one column of a databound DataGridView.
For that I have applied the technique given in "How to: Customize Data Formatting in the Windows Forms DataGridView Control".
My code is below.
When running this with a moderate size dataset (200 rows), everything goes well.
However, when trying to load a larger dataset (2000 rows), after about 400 rows, I get a StackOverflowException right on the line where a bitmap is assigned to the cell value.
Note that I load the bitmaps from the resources.
Does this mean that the bitmaps are duplicated for each row they are used in making the application to go in StackOverflow?
Is there a cleaner way to work?
Thanks for your help in advance.
public Form1()
{
InitializeComponent();
...
circleGrayImage = MyAppNameSp.Properties.Resources.imr_CircleGray_16;
circleGreenImage = MyAppNameSp.Properties.Resources.imr_CircleGreen_16;
circleRedImage = MyAppNameSp.Properties.Resources.imr_CircleRed_16;
}
private void openToolStripButton_Click(object sender, EventArgs e)
{
...
// Bind dsStringDataSet to dataGridView
dataGridView.DataSource = dsStringDataSet;
dataGridView.DataMember = "string";
// Add unbound column for displaying icons
DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
imageColumn.Name = "icon";
imageColumn.HeaderText = "";
dataGridView.Columns.Insert(2, imageColumn);
...
}
private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView.Rows.Count > 1)
{
if (dataGridView.Columns[e.ColumnIndex].Name.Equals("icon"))
{
DataGridViewCell iconCell = dataGridView["icon", e.RowIndex];
iconCell.Style.NullValue = null;
String stringValue = dataGridView[sTargetLangCodeTS, e.RowIndex].Value as string;
switch (stringValue)
{
case sTS_Translated:
iconCell.Value = circleGreenImage; // <<< StackOverflowException here
iconCell.ToolTipText = "Translated";
break;
case sTS_Untranslated:
iconCell.Value = circleRedImage;
iconCell.ToolTipText = "Untranslated";
break;
case sTS_NotNeeded:
iconCell.Value = circleGrayImage;
iconCell.ToolTipText = "No translation needed";
break;
default:
iconCell.Value = null;
iconCell.ToolTipText = String.Empty;
break;
}
}
}
}