C# 从 DataTable 填充时如何在 DataGridView 图像列中禁用空图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11782112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable Null image in DataGridView image column when populated from DataTable
提问by Jim
I have an existing application with a new requirement to show an image in a DataGridView cell to denote whether the record has a specific flag associated with it or not (not user editable, this comes from the DB).
我有一个现有的应用程序,它有一个新要求,即在 DataGridView 单元格中显示图像,以表示该记录是否具有与其关联的特定标志(用户不可编辑,这来自数据库)。
If there is a flag, I show the corresponding image, and if there's no flag, I want nothing to be shown in the column.
如果有标志,我显示相应的图像,如果没有标志,我不希望在列中显示任何内容。
The DataGridView columns weren't created in Visual Studio designer, or else this would easy. I could just set the NullValue property on the column. Instead the columns are created at runtime when all the data is loaded into a DataTable, and then a DataView is created from that DataTable, and then the DataGridView's Datasource is set to the DataView.
DataGridView 列不是在 Visual Studio 设计器中创建的,否则这很容易。我可以在列上设置 NullValue 属性。而是在运行时将所有数据加载到 DataTable 中时创建列,然后从该 DataTable 创建 DataView,然后将 DataGridView 的 Datasource 设置为 DataView。
I can't completely rewrite this, or else I'd just define the columns in VS Designer instead of this ridiculous way of just letting the columns be defined from the DataTable.
我不能完全重写这个,否则我只是在 VS Designer 中定义列,而不是这种让列从 DataTable 定义的荒谬方式。
My question is then, how can I make it so the column with the Images shows nothing when the underlying data table has a null?
我的问题是,当基础数据表为空时,如何才能使带有图像的列不显示任何内容?
Here's some pseudo C# to demonstrate what I mean. Remember, I didn't write it to use two DataTables like this; it was that way when I had it handed to me, and I don't want to make drastic changes just to add a new column...
这里有一些伪 C# 来演示我的意思。记住,我写它不是为了使用这样的两个数据表;当我把它交给我时就是这样,我不想仅仅为了添加一个新列而进行剧烈的改变......
DataTable rawData = someMethodThatReturnsMyRawData();
DataTable data = new DataTable();
data.Columns.Add("Flags", typeof(Image));
data.Columns.Add("SomeOtherColumn");
foreach (DataRow rawDataRow in rawData.Rows)
{
DataRow dataRow = data.NewRow();
bool hasFlagType1 = false;
bool hasFlagType2 = false;
if (rawDataRow["FlagType1ID"] != DBNull.Value)
{
hasFlagType1 = true;
}
if (rawDataRow["FlagType2ID"] != DBNull.Value)
{
hasFlagType2 = true;
}
if (hasFlagType1 && hasFlagType2)
{
dataRow[0] = Properties.Resources.BothFlagsImage;
}
else if (hasFlagType1)
{
dataRow[0] = Properties.Resources.FlagType1Image;
}
else if (hasFlagType2)
{
dataRow[0] = Properties.Resources.FlagType2Image;
}
else
{
//If neither flag set, I don't want it to show anything,
//but if I leave it as null, a little box with an X in it shows up
//I could set it to some transparent GIF here, but that seems lame
}
dataRow[1] = rawDataRow["SomeOtherColumn"];
data.Rows.Add(dataRow);
}
DataView dv = new DataView(data, string.Empty, "SomeOtherColumn ASC", DataViewRowState.CurrentRows);
this.emptyDataGridViewFromDesigner.DataSource = dv;
// How can I modify the column "Flags" at this point to show nothing if the value is null?
EDIT: Here's a screenshot so you can see what I mean by the little box with an X - those are all nulls...
编辑:这是一个屏幕截图,因此您可以看到我所说的带有 X 的小框的意思 - 这些都是空值...


Also, it has to be .NET 3.5, so if there's a solution in .NET 4.0 only, I'm out of luck.
此外,它必须是 .NET 3.5,所以如果只有 .NET 4.0 有解决方案,我就不走运了。
采纳答案by Jim
I figured this out...
我想通了这一点...
Have to cast the column as a DataGridViewImageColumn, then set the DefaultCellStyle.NullValue for that column to null. From my example above, you'd do it like this...
必须将该列转换为 DataGridViewImageColumn,然后将该列的 DefaultCellStyle.NullValue 设置为 null。从我上面的例子中,你会这样做......
((DataGridViewImageColumn)this.emptyDataGridViewFromDesigner.Columns["Flags"]).DefaultCellStyle.NullValue = null;
I guess I jumped the gun asking here, but hope this helps someone else sometime.
我想我在这里问过,但希望这有助于其他人。
回答by Alex.Uvarov
To fix whole grid, just add this code to Form constructor. (and change name of your dataGrid):
要修复整个网格,只需将此代码添加到 Form 构造函数即可。(并更改数据网格的名称):
Load += delegate
{
// remove default [x] image for data DataGridViewImageColumn columns
foreach (var column in dataGridView1.Columns)
{
if (column is DataGridViewImageColumn)
(column as DataGridViewImageColumn).DefaultCellStyle.NullValue = null;
}
};
回答by NetXpert
It's FAR easier to simply assign new Bitmap(1,1);to the cell's .Valueproperty and move on. My app was throwing exceptions whenever I tried to assign NULL to the Cell's Value, even with the modified DefaultCellStyle.NullValue
简单地分配new Bitmap(1,1);给单元格的.Value属性并继续前进要容易得多。每当我尝试将 NULL 分配给 Cell 的值时,我的应用程序都会抛出异常,即使修改了DefaultCellStyle.NullValue
Something like this works as intended, every time, without any hassles or arcane/obscure settings:
这样的事情每次都按预期工作,没有任何麻烦或神秘/晦涩的设置:
dataGridView1.Rows[index].Cells["CellName"].Value = isFlag ? Properties.Resources.FlagImage : new Bitmap(1,1);

