集合被修改;将单选按钮添加到动态创建的 Obout 网格时,枚举操作可能不会执行异常?
Collection was modified; enumeration operation might not execute exception when adding a radio button to a dynamically created Obout grid?
我尝试将单选按钮添加到数据源,因为我不知道如何将单选按钮添加到动态 Obout 网格,而且我收到 Collection was modified; enumeration operation might not execute
异常。有没有办法以不同的方式将它添加到 DataTable 中?或者我应该尝试将单选按钮添加到列表中,然后直接添加列表而不是单选按钮?
public Grid gridTickets = new Grid(); //Obout grid initializing code
Column id = new Column();//Creating the Obout grid columns
id.DataField = "id";
id.HeaderText = "Ticket Nr";
id.Width = globals.SecurityKey == 5 ? "10%" : "8%";
Column severity = new Column();
severity.DataField = "severity";
severity.HeaderText = "Severity";
severity.Width = "10%";
gridTickets.Columns.Add(id);//Adding the columns to the Obout grid
gridTickets.Columns.Add(severity);
placeHere.Controls.Add(gridTickets);//Add the OboutGrid to the Place holder on the aspx side
dt = helpers.DisplayTickets();
RadioButton rb = new RadioButton();
int i = 1;
foreach (DataRow r in dt.Rows)
{
string temp = r[1].ToString();
if (r[].ToString().Contains("Level"))
{
rb.ID = "rb" + i;
dt.Rows.Add(r[1].ToString() + rb);
}
i++;
}
gridTickets.DataSource = dt;//Adding the DataTable to the Obout grid DataSource
gridTickets.DataBind();
您可以像 Klaus 在评论中提到的那样遍历行的临时快照,或者使用常规 for 循环的替代方法:
for (int x = dt.Rows.Count-1; x >= 0; x--)
{
DataRow r = dt.Rows[x];
string temp = r[4].ToString();
if (r[1].ToString().Contains("Level"))
{
rb.ID = "rb" + i;
dt.Rows.Add(r[4].ToString() + rb);
}
i++;
}
我们向后迭代,这样我们就不会在循环期间结束处理添加到集合末尾的行。此方法还可以让您更好地控制将记录插入 table 的位置,例如,如果您想将它们插入“刚好在正在处理的行之后”[=12=]
注意;你也可以使用这种形式来摆脱 i
- 如果它能提供唯一性,x 也可以做到这一点
大家好,谢谢大家的精彩回复,他们对我帮助很大,我最终使用了一个很棒的字体图标,并将其作为 Lable
添加到 DataBound
方法中。甚至让它只在条目的解决日期已经过期时显示。
这就是网格的建设
public Grid gridTickets = new Grid(); //Obout grid initializing code
Column id = new Column();//Creating the Obout grid columns
id.DataField = "id";
id.HeaderText = "Ticket Nr";
id.Width = globals.SecurityKey == 5 ? "10%" : "8%";
Column severity = new Column();
severity.DataField = "severity";
severity.HeaderText = "Severity";
severity.Width = "10%";
gridTickets.ID = "gridTickets";//Grid settings
gridTickets.RowDataBound += new GridRowEventHandler(OnGridDataBound);// adding a DataBound method
gridTickets.Columns.Add(id);//Adding the columns to the Obout grid
gridTickets.Columns.Add(severity);
placeHere.Controls.Add(gridTickets);//Add the OboutGrid to the Place holder on the aspx side
DataTable dt = helpers.DisplayTickets();
gridTickets.DataSource = dt;//Adding the DataTable to the Obout grid DataSource
gridTickets.DataBind();
这就是方法
protected void OnGridDataBound(object sender, GridRowEventArgs e)
{
Label lb = new Label();
lb.Attributes.Add("class", "fa fa-circle");
lb.Attributes.Add("style", "color:red; float:right; margin-top:-17px; margin-right:10px;");
for (int i = 0; i < e.Row.Cells.Count; i++)
{
DateTime checkdate = Convert.ToDateTime(e.Row.Cells[7].Text);
DateTime checkdateL1 = checkdate.AddHours(6);
DateTime checkdateL2 = checkdate.AddHours(12);
DateTime checkdateL3 = checkdate.AddHours(48);
if (e.Row.Cells[i].Text == "Level 1" && DateTime.Now >= checkdateL1)
{
e.Row.Cells[2].Controls.Add(lb);
}
if (e.Row.Cells[i].Text == "Level 2" && DateTime.Now >= checkdateL2)
{
e.Row.Cells[2].Controls.Add(lb);
}
if (e.Row.Cells[i].Text == "Level 3" && DateTime.Now >= checkdateL3)
{
e.Row.Cells[2].Controls.Add(lb);
}
}
}
我尝试将单选按钮添加到数据源,因为我不知道如何将单选按钮添加到动态 Obout 网格,而且我收到 Collection was modified; enumeration operation might not execute
异常。有没有办法以不同的方式将它添加到 DataTable 中?或者我应该尝试将单选按钮添加到列表中,然后直接添加列表而不是单选按钮?
public Grid gridTickets = new Grid(); //Obout grid initializing code
Column id = new Column();//Creating the Obout grid columns
id.DataField = "id";
id.HeaderText = "Ticket Nr";
id.Width = globals.SecurityKey == 5 ? "10%" : "8%";
Column severity = new Column();
severity.DataField = "severity";
severity.HeaderText = "Severity";
severity.Width = "10%";
gridTickets.Columns.Add(id);//Adding the columns to the Obout grid
gridTickets.Columns.Add(severity);
placeHere.Controls.Add(gridTickets);//Add the OboutGrid to the Place holder on the aspx side
dt = helpers.DisplayTickets();
RadioButton rb = new RadioButton();
int i = 1;
foreach (DataRow r in dt.Rows)
{
string temp = r[1].ToString();
if (r[].ToString().Contains("Level"))
{
rb.ID = "rb" + i;
dt.Rows.Add(r[1].ToString() + rb);
}
i++;
}
gridTickets.DataSource = dt;//Adding the DataTable to the Obout grid DataSource
gridTickets.DataBind();
您可以像 Klaus 在评论中提到的那样遍历行的临时快照,或者使用常规 for 循环的替代方法:
for (int x = dt.Rows.Count-1; x >= 0; x--)
{
DataRow r = dt.Rows[x];
string temp = r[4].ToString();
if (r[1].ToString().Contains("Level"))
{
rb.ID = "rb" + i;
dt.Rows.Add(r[4].ToString() + rb);
}
i++;
}
我们向后迭代,这样我们就不会在循环期间结束处理添加到集合末尾的行。此方法还可以让您更好地控制将记录插入 table 的位置,例如,如果您想将它们插入“刚好在正在处理的行之后”[=12=]
注意;你也可以使用这种形式来摆脱 i
- 如果它能提供唯一性,x 也可以做到这一点
大家好,谢谢大家的精彩回复,他们对我帮助很大,我最终使用了一个很棒的字体图标,并将其作为 Lable
添加到 DataBound
方法中。甚至让它只在条目的解决日期已经过期时显示。
这就是网格的建设
public Grid gridTickets = new Grid(); //Obout grid initializing code
Column id = new Column();//Creating the Obout grid columns
id.DataField = "id";
id.HeaderText = "Ticket Nr";
id.Width = globals.SecurityKey == 5 ? "10%" : "8%";
Column severity = new Column();
severity.DataField = "severity";
severity.HeaderText = "Severity";
severity.Width = "10%";
gridTickets.ID = "gridTickets";//Grid settings
gridTickets.RowDataBound += new GridRowEventHandler(OnGridDataBound);// adding a DataBound method
gridTickets.Columns.Add(id);//Adding the columns to the Obout grid
gridTickets.Columns.Add(severity);
placeHere.Controls.Add(gridTickets);//Add the OboutGrid to the Place holder on the aspx side
DataTable dt = helpers.DisplayTickets();
gridTickets.DataSource = dt;//Adding the DataTable to the Obout grid DataSource
gridTickets.DataBind();
这就是方法
protected void OnGridDataBound(object sender, GridRowEventArgs e)
{
Label lb = new Label();
lb.Attributes.Add("class", "fa fa-circle");
lb.Attributes.Add("style", "color:red; float:right; margin-top:-17px; margin-right:10px;");
for (int i = 0; i < e.Row.Cells.Count; i++)
{
DateTime checkdate = Convert.ToDateTime(e.Row.Cells[7].Text);
DateTime checkdateL1 = checkdate.AddHours(6);
DateTime checkdateL2 = checkdate.AddHours(12);
DateTime checkdateL3 = checkdate.AddHours(48);
if (e.Row.Cells[i].Text == "Level 1" && DateTime.Now >= checkdateL1)
{
e.Row.Cells[2].Controls.Add(lb);
}
if (e.Row.Cells[i].Text == "Level 2" && DateTime.Now >= checkdateL2)
{
e.Row.Cells[2].Controls.Add(lb);
}
if (e.Row.Cells[i].Text == "Level 3" && DateTime.Now >= checkdateL3)
{
e.Row.Cells[2].Controls.Add(lb);
}
}
}