[英]Winforms DataGridView CellEndEdit Loops
For an assignment, we're creating a simple spreadsheet program, similar to Excel. 对于作业,我们正在创建一个简单的电子表格程序,类似于Excel。 The cells in my DataGridView are editable by the user, and multiple things happen when the user edits a cell.
用户可以编辑DataGridView中的单元格,并且用户编辑单元格时会发生多种情况。 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. 与Excel相似,如果要在单元格A2中键入
=A1
,则A2的值将更改为等于单元格A1的值,并且用户看到的文本会更新以反映这一点。 However, A2's text property needs to remain as =A1
in case A1's value changes. 但是,如果A1的值发生更改,则A2的text属性需要保留为
=A1
。 While debugging my code, I noticed that my CellEndEdit
function was looping through twice, although there are no loops involved. 在调试代码时,我注意到我的
CellEndEdit
函数循环了两次,尽管没有涉及循环。
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? CellEndEdit
函数是否应该循环两次? How can I stop this from happening? 我该如何阻止这种情况的发生? Thanks!
谢谢!
CellEndEdit
will be triggered when changing the cell value. 更改单元格值时将触发
CellEndEdit
。
So in your case, input value to A1, trigger CellEndEdit
因此,在您的情况下,将值输入A1,触发
CellEndEdit
Then your code set A2 cell = A1's value, trigger another CellEndEdit
然后您的代码设置A2单元格= A1的值,触发另一个
CellEndEdit
You can add a bool to control if you need to suppress the event manually. 您可以添加布尔值来控制是否需要手动取消事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.