【发布时间】:2016-03-27 13:29:42
【问题描述】:
我正在开发一个使用DataGridView 控件的Windows 窗体应用程序。这个DataGridView 有一个包含按钮的单元格。默认按钮文本是 Start 但当我尝试动态更改它时:
((DataGridViewButtonCell)Myrow.Cells[9]).Value = "End";
或者只是喜欢
Myrow.Cells[9].Value = "End";
它抛出以下异常:
An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll
我的完整代码是这样的:
void HighlightOnlineUsers()
{
foreach (DataGridViewRow Myrow in dataGridView1.Rows)
{
if (Convert.ToInt32(Myrow.Cells[8].Value) > 0)
{
Myrow.DefaultCellStyle.BackColor = Color.GreenYellow;
Myrow.Cells[9].Value = "End";
}
else {
Myrow.DefaultCellStyle.BackColor = Color.White;
((DataGridViewButtonCell)Myrow.Cells["SessionAction"]).Value = "Start";
}
}
}
【问题讨论】:
-
问题是你从哪里打电话给
HighlightOnlineUsers -
@IvanStoev 它在
Form的代码文件中,DataGridView位于其中。 -
我的意思是,看起来你是从某个事件中调用它,并且实现触发了相同的事件,因此 StackOverflowException。
-
@IvanStoev 我从
dataGridView1_CellFormatting事件中调用它。也来自其他改变数据的事件。 -
这就是导致异常的原因,每次格式更新都会触发
dataGridView1_CellFormatting事件,当您在此方法中更新CellFormat时,这会产生递归。
标签: c# .net winforms button datagridview