我提出这个问题是因为在网上找到答案花了太长时间,这可能是一个常见问题 - 这是我第二次在我的应用程序中遇到它。
当带有 DataGridViewImageCell 的新行变得可见,并且没有设置默认值时,我的 DataGridView 会抛出以下异常:
The Following Exception occurred in the DataGridView:
System.ArgumentException: Parameter is not valid. at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)"
在我的设置中,我在 Visual Studio Designer 中创建了 DataGridViewImageColumns,然后通过设置 DataGridViewImageColumns 的 DataPropertyName 属性以匹配类型为 byte[] 的 DataColumns,将这些列绑定(bind)到 DataTable 中的 DataColumns。
但是,当新行中的 DataGridViewImageColumn 变得可见时,它仍然会抛出此异常。
有两种解决方法对我有用:
像这样处理 DataGridView 的 DataError 事件:
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == DBNull.Value)
{
e.Cancel = true;
}
}
这是我现在要使用的选项,但我不喜欢抑制异常,而且我可以看到由于处理程序的 throw + Catch 而导致创建 DataGridView 行的延迟。
MSDN ( http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewimagecolumn.aspx ) 表示您可以处理 RowsAdded 事件并强制使用空值。我试过这个:
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
foreach (DataGridViewCell cell in dataGridView1.Rows[e.RowIndex].Cells)
{
if (cell.GetType() == typeof(DataGridViewImageCell))
{
cell.Value = DBNull.Value;
}
}
}
...这没用。
另一个选项涉及将 Column CellTemplate 设置为从 DataGridViewImageColumn 派生的类型,默认值为 null 或 DBNull.Value。
现在有点晚了 - 我已经做了一整天了。
我可能会选择选项 2,但谁能告诉我如何让选项 3/4 生效?对此有最佳方法吗?
最佳答案
我的解决办法:添加完列就直接删除(详细原因在文末)。以下代码删除了所有可能的图像列,如果您的架构不是动态的并且您知道要删除的内容,您可能需要自定义此列:
public Form1()
{
InitializeComponent();
dataGridView1.ColumnAdded += dataGrid_ColumnAdded;
}
void dataGrid_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{
if (e.Column.CellType == typeof(DataGridViewImageCell))
dataGridView1.Columns.Remove(e.Column);
}
所以说到实际的绑定(bind)
DataTable table = dataTableCombo.SelectedItem as DataTable;
dataGridView1.DataSource = table;
填充单元格将在添加(和删除更正)列之后发生。异常不会以这种方式发生。
此外,在您的 dataGridView1_RowsAdded 事件处理程序中注意:不仅有 e.RowIndex,还有 e.RowCount,它可以是 e.RowCount > 1!所以首先我尝试了:
void dataGrid_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
for (int i = e.RowIndex; i < e.RowIndex + e.RowCount; i++)
{
foreach (DataGridViewCell cell in dataGridView1.Rows[i].Cells)
{
if (cell.GetType() == typeof(DataGridViewImageCell))
{
cell.Value = DBNull.Value;
}
}
}
}
但我仍然有一些异常(exception)。另外,如果您的绑定(bind)是双向的,请注意,因为 cell.Value = DBNull.Value; 会导致业务对象发生变化!这就是我建议删除该列的原因。
关于c# - 由于 DataGridViewImageColumn,DataGridView 在默认错误对话框中抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19476488/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option
我正在学习Rails,并阅读了关于乐观锁的内容。我已将类型为integer的lock_version列添加到我的articles表中。但现在每当我第一次尝试更新记录时,我都会收到StaleObjectError异常。这是我的迁移:classAddLockVersionToArticle当我尝试通过Rails控制台更新文章时:article=Article.first=>#我这样做:article.title="newtitle"article.save我明白了:(0.3ms)begintransaction(0.3ms)UPDATE"articles"SET"title"='dwdwd
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我早就知道Ruby中的“常量”(即大写的变量名)不是真正常量。与其他编程语言一样,对对象的引用是唯一存储在变量/常量中的东西。(侧边栏:Ruby确实具有“卡住”引用对象不被修改的功能,据我所知,许多其他语言都没有提供这种功能。)所以这是我的问题:当您将一个值重新分配给常量时,您会收到如下警告:>>FOO='bar'=>"bar">>FOO='baz'(irb):2:warning:alreadyinitializedconstantFOO=>"baz"有没有办法强制Ruby抛出异常而不是打印警告?很难弄清楚为什么有时会发生重新分配。 最佳答案
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.