Laurent
Laurent

Reputation: 733

Datagridview : DataGridViewImageColumn displays a red cross

I have a datagridview fed by a datatabe to which I have added an extra dataGridViewImageColumn. However, instead of displaying the correct flag (picture) in the added column, a red cross appears.

I have googled the entire afternoon for this problem, still wondering what I am doing wrong here. Any idea?

Thanks in advance for your help.

    private void displayMeteoCities()
    {
        DataTable l_Table = null;
        ClsCountriesFactory l_Countries = null;
        DataGridViewImageColumn l_ImageColumn = null;
        Bitmap l_Flag = null;
        string l_Country = string.Empty;

        try
        {
            this.m_Meteo_Cities = new ClsMeteoCitiesFactory();
            this.m_Meteo_Cities.getMeteoCities();
            l_Table = ClsDomoosManagerCore.CreateDataTable(this.m_Meteo_Cities.Meteo_Cities);
            this.dgr_Meteo_Cities.DataSource = l_Table;


            l_ImageColumn = new DataGridViewImageColumn();
            // l_ImageColumn.DisplayIndex = 0;
            l_ImageColumn.Width = 50;
            l_ImageColumn.Name = "Country_flag";
            //l_ImageColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.dgr_Meteo_Cities.Columns.Add(l_ImageColumn);

            l_Countries = new ClsCountriesFactory();
            l_Countries.loadCountryFlags();

            this.dgr_Meteo_Cities.RowsDefaultCellStyle.BackColor = Color.Moccasin;
            this.dgr_Meteo_Cities.AlternatingRowsDefaultCellStyle.BackColor = Color.GreenYellow;

            for (int i = 0; i < this.dgr_Meteo_Cities.Rows.Count; i++)
            {
                l_Country = Convert.ToString(this.dgr_Meteo_Cities["iso_country", i].Value);
                l_Flag = l_Countries.Flags[l_Country];
                this.dgr_Meteo_Cities[7, i].Value = l_Flag;
            }

        }
        catch (Exception exc)
        {
            ClsErrorManager.manageException(exc);
        }
        finally
        {
            l_Table = null;
            l_Flag = null;
        }
}

Upvotes: 2

Views: 871

Answers (1)

RazorKillBen
RazorKillBen

Reputation: 573

I know it's an old question, but in the hopes of solving someones frustration in the future...

Whilst I'd almost always recommend against fiddling with Controls Constructors when using the Designer, in this case it's the correct way of handling it. Find the image column (not the DataGridView) in the InitializeComponent routine, and add the below line, replacing ImageColumn with the name of your image column:

VB.Net

Me.ImageColumn.DefaultCellStyle.NullValue = Nothing

C#

{
    this.ImageColumn.DefaultCellStyle.NullValue = null;
}

Upvotes: 2

Related Questions