Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > StackOverflowException assigning a bitmap to a cell value in DataGridView_CellFormatting
 

StackOverflowException assigning a bitmap to a cell value in DataGridView_CellFormatting

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;
            }
        }
    }
}

JcCar  Wednesday, September 02, 2009 4:18 PM
Alright. Found the problem thanks to Guulh.

This is not the cell that must be assigned but eventArg e. Otherwise event CellFormatting is fired again and again leading to the StackOverflowException.

All "iconCell.Value = ..." lines must be replaced by "e.Value = ..." in the above code and then it works perfectly.
JcCar  Thursday, September 03, 2009 4:01 PM
Alright. Found the problem thanks to Guulh.

This is not the cell that must be assigned but eventArg e. Otherwise event CellFormatting is fired again and again leading to the StackOverflowException.

All "iconCell.Value = ..." lines must be replaced by "e.Value = ..." in the above code and then it works perfectly.
JcCar  Thursday, September 03, 2009 4:01 PM

You can use google to search for other answers

Custom Search

More Threads

• Connecting to Access database
• Bound datagridview failing to update
• select row in gridview
• help! Last minute issue - rookie DB question
• problem of binding combobox to datatable
• Date error in dateTimePicker Binding
• DataSet row changed
• Cannot access disposed object named 'DataGridTextBox'
• Master pagesh how
• Beginner Formatting Questions