Datagridview:DataGridViewImageColumn显示红叉

时间:2016-12-28 16:52:25

标签: c# datagridview

我有一个datagridview由一个dataatabe提供,我已经添加了一个额外的dataGridViewImageColumn。但是,不是在添加的列中显示正确的标记(图片),而是出现红叉。

我已经在整个下午搜索了这个问题,仍然想知道我在这里做错了什么。有什么想法吗?

提前感谢您的帮助。

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

1 个答案:

答案 0 :(得分:1)

我知道这是一个古老的问题,但是希望能解决将来遇到的挫折...

尽管我几乎总是建议不要在使用Designer时摆弄Controls构造函数,但在这种情况下,这是处理它的正确方法。在DataGridView例程中找到图像列(而不是InitializeComponent),然后添加以下行,将ImageColumn替换为图像列的名称:

VB.Net

Me.ImageColumn.DefaultCellStyle.NullValue = Nothing

C#

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