开发者

SetCurrentCellAddressCore DatagridView exception

开发者 https://www.devze.com 2023-03-18 22:25 出处:网络
I have a DataGridView that shows a list of cross reference part numbers, each with a company name and a cross reference number. When the user finishes editing a cell it automatically sends an update.

I have a DataGridView that shows a list of cross reference part numbers, each with a company name and a cross reference number. When the user finishes editing a cell it automatically sends an update. However, I obviously don't want empty cross references stored in the table, so if the user deletes the information from both the company and number fields, I just delete that record. It's all fine on the SQL end of business but I'm having removing the row from the dgv. Whenever you try to remove a row in the end edit, it poops out

Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.

I have seen some resources through googling that shed little light on the issue. I can't seem to figure out a way around this. I have tried the dgv.Rows.Remove() and I have tried just running my FillDGV() method which clears the dgv at the beginning, but it seems like it has some major problems deleting the parent row of the cell that is firing the EndEdit event. I have tried some things that other people have suggested, like giving another control focus before the remove and changing the current cell but neither work. Can anyone suggest a work around for this?

private void viewCrossRef_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    string number = null;
    string company = null;

    if (viewCrossRef["CrossReference", e.RowIndex].Value != null)
        number = viewCrossRef["CrossReference", e.RowIndex].Value.ToString();
    if (viewCrossRef["Company", e.RowIndex].Value != null)
        company = viewCrossRef["Company", e.RowIndex].Value.ToString();

    basePart.CrossReferences[e.RowIndex].Company = company;
    basePart.CrossReferences[e.RowIndex].CrossReferenceNumber = number;

    if (String.IsNullOrEmpty(company) && String.IsNullOrEmpty(number))
        viewCrossRef.Rows.R开发者_开发知识库emoveAt(e.RowIndex);
    basePart.UpdateCrossReferences();
}

The DataTable is updated in the UpdateCrossReferences method. It dies on the row removal.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号