简体   繁体   中英

Winforms DataGridView CellEndEdit Loops

For an assignment, we're creating a simple spreadsheet program, similar to Excel. The cells in my DataGridView are editable by the user, and multiple things happen when the user edits a cell. Similar to Excel, if you were to type =A1 into cell A2, A2's value changes to be equal to the value of cell A1, and the text that the user sees updates to reflect this. However, A2's text property needs to remain as =A1 in case A1's value changes. While debugging my code, I noticed that my CellEndEdit function was looping through twice, although there are no loops involved.

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        RealCell cell = spreadsheet.GetCell(e.RowIndex, e.ColumnIndex) as RealCell;

        //set the spreadsheet cell's text to reflect the user's edit
        if (dataGridView1.CurrentCell.Value != null)
        {
            cell.Text = dataGridView1.CurrentCell.Value.ToString();
        }
        MenuVisibility();
    }

This is a big problem, because it resets the cell's text once more than it should. Are CellEndEdit functions supposed to loop through twice? How can I stop this from happening? Thanks!

CellEndEdit will be triggered when changing the cell value.

So in your case, input value to A1, trigger CellEndEdit

Then your code set A2 cell = A1's value, trigger another CellEndEdit

You can add a bool to control if you need to suppress the event manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM