datatable.GetChanges() shows always first row of D

2019-06-12 12:59发布

问题:

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