How to dynamically set color code groups in gridview?

60 Views Asked by At

I have a gridview that looks like below...

enter image description here

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

}

1

There are 1 best solutions below

0
SyndRain On

Your LastMRN variable 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.

<asp:GridView ID="GridView1" runat="server" OnDataBound="GridView1_DataBound">


protected void GridView1_DataBound(object sender, EventArgs e)
{
    bool isGray = false;
    string prevNumber = “”;
    for(int i = 0; i<GridView1.Rows.Count; i++)
    {
        GridViewRow row = GridView1.Rows[i];
        if (row.Cells[2].Text != prevNumber)
        {
            isGray = !isGray;
        }
        row.BackColor = isGray ? System.Drawing.Color.Gray : System.Drawing.Color.LightBlue;
        prevNumber = row.Cells[2].Text;
    }
}

Alternatively, You can still use the RowDataBound event and read the patient number and color of the previous row instead. (Or you can store lastMRN and lastColor outside the function, though a bit more messy.)

protected void grdPatientList_RowDataBound(object sender, GridViewRowEventArgs e)
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if(e.Row.RowIndex == 0)
        {
            e.Row.BackColor = System.Drawing.Color.Gray;
        }
        else
        {
            System.Drawing.Color prevColor = GridView1.Rows[e.Row.RowIndex - 1].BackColor;
            string prevNumber = GridView1.Rows[e.Row.RowIndex - 1].Cells[2].Text;
            if (e.Row.Cells[2].Text == prevNumber)
            {
                e.Row.BackColor = prevColor;
            }
            else
            {
                e.Row.BackColor = prevColor == System.Drawing.Color.Gray ? System.Drawing.Color.LightBlue : System.Drawing.Color.Gray;
            }
        }
    }
}