c# - 数据 GridView : DataGridViewImageColumn displays a red cross

标签 c# datagridview

我有一个由数据表提供的数据 GridView ,我在其中添加了一个额外的数据 GridView 图像列。但是,添加的列中没有显示正确的标志(图片),而是出现了红叉。

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

预先感谢您的帮助。

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

最佳答案

我知道这是一个老问题,但希望能解决将来某人的挫败感......

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

VB.Net

Me.ImageColumn.DefaultCellStyle.NullValue = Nothing

C#

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

关于c# - 数据 GridView : DataGridViewImageColumn displays a red cross,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41366391/

相关文章:

c# - 静态类是否应该在 "static destructor"中处理其 IDisposable 变量?

c# - 随机无效 View 状态错误

c# - 如何恢复与 WCF 的连接?

c# - WPF - DataGrid 不应处理 Ctrl+A

c# - gridview 中的 cellendedit 事件在另一个 gridview 中添加行?

c# - 在 dataGridView 中的单个列下显示不同表中的两个属性

c# - 在 ASP.NET 中检测/估计 http 请求的国家/地区

winforms - DataGridViewImageColumn 红色 x

c# - 有没有更好的方法来过滤这个datagridview?

apache-flex - 动态更改 FLEX 中 Datagrid 列的宽度