0

I tried adding the radio button to the data source as I have no idea how to add the radio button to a dynamic Obout grid and I'm receiving a Collection was modified; enumeration operation might not execute exception. is there a way to add it to the DataTable a different way? or should I try adding the radio button to a list and then adding the list instead of the radio button directly?

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();

2 Answers2

0

You can loop over a temporary snapshot of the rows as Klaus mentioned in comment, or an alternative is to use a regular for loop:

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++;
}

We iterate backwards so that we don't end up processing rows added to the end of the collection during the loop. This method can also give you more control over where to insert records into the table, if for example you wanted to insert them at "just after the row being processed"

Note; you might also be able to use this form to get rid of i - if all it does it provide uniqueness, x can do that too

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • sorry it should be r[1] as the array starts with 0. There are a lot more columns in my og code and just for got to change it for the code here. it will give a syntax error if the column doesn't exist – Madeline Kallis Mar 25 '21 at 09:02
  • Corrected in line with your edit to the question – Caius Jard Mar 25 '21 at 11:34
0

Hi guys thank you all for the awesome responses they helped me a lot I ended up using a Font awesome icon and adding that as a Lable in DataBound method. And even made it only show when an entry's resolution date has already expired.

This Is the building of the grid

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();

And this is the method

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);
        }
    }
}