0

我尝试将单选按钮添加到数据源,因为我不知道如何将单选按钮添加到动态 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();
4

2 回答 2

0

您可以像 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++;
}

我们向后迭代,这样我们就不会在循环期间处理添加到集合末尾的行。此方法还可以让您更好地控制将记录插入表的位置,例如,如果您想在“正在处理的行之后”插入它们

笔记; 您也许还可以使用这种形式来摆脱i- 如果它所做的一切都提供了唯一性,那么 x 也可以做到这一点

于 2021-03-25T07:11:48.210 回答
0

大家好,谢谢大家的精彩回复,他们给了我很多帮助,我最终使用了一个很棒的字体图标并将其添加为LableinDataBound方法。甚至让它只显示条目的解决日期已经过期。

这是网格的构建

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);
        }
    }
}
于 2021-03-26T12:16:02.523 回答