datatable.GetChanges()始终显示DataGridView的第一行 [英] datatable.GetChanges() shows always first row of DataGridView
问题描述
我有一个DataGridView,其绑定源绑定到从Oracle数据库加载的数据表。 (顺便说一句,我不认为数据库连接会导致这种情况)
I have a DataGridView with a Binding-Source bound to a Data-Table loaded from an Oracle-Database. (btw. I don't think that the Database-Connection could cause this)
我还有一个绑定到一类人的DataGridViewComboBoxColumn(只有具有 ID和名称),因此我可以显示/允许编辑名称而不是ID。现在我的问题是,数据绑定完成后,c#自动选择 DGV的第一个单元格-请参见所附图片。
I have also a DataGridViewComboBoxColumn bound to an class of persons (only "has" ID and name), so I can display / allow to edit the name instead of the ID. My problem is now that after the data-binding is completed c# automatically "selects" the first cell of the DGV - see the attached picture.
我还必须使用这段代码来确保数据完整性:
I also have to use this little piece of code to ensure data integrity:
private void _table_ColumnChanging(object sender, DataColumnChangeEventArgs e)
{
if (e.Column == _table.Columns["NEEDED_ID"])
{
if (e.ProposedValue == null)
{
e.ProposedValue = System.DBNull.Value;
}
}
}
现在使用此_table.GetChanges ()始终将DGV的第一行返回为已修改,但该值并未真正更改-只是 DBNull
而不是 null
。我能以某种方式避免自动选择第一个单元格或如何避免这种行为吗?
Now using this _table.GetChanges() always returns the first row of the DGV as "modified" but the value isn't really changed - it's only DBNull
instead of null
. Can I somehow avoid automatic selection of first cell or how to avoid this behavior?
编辑:同时,我发现将第一列更改为无法编辑的内容可以解决此问题。但这不过是一种解决方法。
Meanwhile I found out that changing the first column to something not editable fixes this problem. But this is nothing more than an workaround. I would really appreciate to get an working solution for this.
编辑2:我在ComboBox的顶部也有一个空对象-DataSource
EDIT 2: I have also an empty Object 'on top' of the ComboBox-DataSource
DtoPerson blankPerson = new DtoPerson();
blankPerson.Name = String.Empty;
blankPerson.PersonRollenId = -1;
personList.Add(blankPerson);
推荐答案
尝试在您的DataGridViewComboBoxColumn的数据源中放置一个空值。例如:
Try to put a null in your DataGridViewComboBoxColumn's DataSource. example:
SELECT id, description
FROM yourTable
UNION
SELECT NULL, NULL
这篇关于datatable.GetChanges()始终显示DataGridView的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!