c# – DataGridView由于DataGridViewImageColumn而导致默认错误
我把它放在一起,因为在网络上找到答案花了太长时间,这可能是一个常见的问题 – 这是我第二次在我的应用程序中体验过.
当具有DataGridViewImageCell的新行变为可见,并且没有设置默认值时,DataGridView会抛出以下异常:
在我的设置中,我在Visual Studio Designer中创建DataGridViewImageColumns,然后通过将DataGridViewImageColumns的DataPropertyName属性设置为匹配DataColumns类型:byte []将这些列绑定到DataTable中的DataColumns. 但是,当新行中的DataGridViewImageColumn变为可见时,它仍会抛出此异常. 有两种解决方法对我有用: >取消选中设计器中的“启用添加”选项,然后以编程方式添加行 – 使用按钮等 – 我认为这是我第一次做的. private void dataGridView1_DataError(object sender,DataGridViewDataErrorEventArgs e) { if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == DBNull.Value) { e.Cancel = true; } } 这是我现在正在进行的选择,但是我不是抑制异常的粉丝,并且可以看到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); } 所以说到实际绑定 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; } } } } 但我还是有一些例外.另外,如果你的绑定是双向的,注意因为cell.Value = DBNull.Value;导致业务对象发生变化!这就是我建议只是删除列的原因. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |