I have a gridview that looks like below...
Dynamically I need to alternate the gridview colors per patient. Patient 1 should have grey color (2 records) Patient 2 should alternate and have blue color (2 records) Patient 3 should alternate and have grey color (2 records) Patient 4 should alternate and have blue color (2 records)
Not all patients have 2 records. Some will have only one while others can have more than 2, but I need the colors to alternate between different patients.
I cannot figure out how to do this...Please can someone help?
I'm using VS 2019, C#, asp.net (this is prior to MVC).
Thanks, Ninel
I tried this:
protected void grdPatientList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
NewMRN = e.Row.Cells[2].Text;
if (counter == 1)
{
LastMRN = NewMRN;
}
if (LastMRN != NewMRN)
{
ColorToggle = true;
mrnvalue = e.Row.Cells[2].Text;
}
else
{
ColorToggle = false;
}
if (ColorToggle == true)
{
e.Row.BackColor = System.Drawing.Color.Gray;
}
else
{
e.Row.BackColor = System.Drawing.Color.LightBlue;
}
LastMRN = NewMRN;
counter++;
}
}

Your
LastMRNvariable is not shared between each event handler function call. One way is to add an OnDataBound event handler which fires after all databinding of the grid are done.Alternatively, You can still use the RowDataBound event and read the patient number and color of the previous row instead. (Or you can store
lastMRNandlastColoroutside the function, though a bit more messy.)