datatable.GetChanges() shows always first row of DataGridView

为君一笑 提交于 2019-12-08 12:13:39

问题


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)

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

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?

EDIT: 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.

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

回答1:


Try to put a null in your DataGridViewComboBoxColumn's DataSource. example:

SELECT id, description
FROM yourTable
UNION
SELECT NULL, NULL



回答2:


GridViewName.ClearSelection();


来源:https://stackoverflow.com/questions/4679897/datatable-getchanges-shows-always-first-row-of-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!