eMRe
eMRe

Reputation: 3247

RowSpan and alternate row color at the same time using c#

I am trying to use row span on 1 column (if the value of the next row is same) and alternate the row color according to the above situation.

I managed to get the row span working the way i wanted but can`t alternate the rows. BatchNo: 3694217 row should be blue aswell.

Here is the current output:

enter image description here

Asp

<asp:GridView ID="visualisation" runat="server" 
                AutoGenerateColumns="false" OnDataBound="OnDataBound" OnItemDataBound="Item_Bound" CellPadding="15" CellSpacing="15" HeaderStyle-BackColor="DarkOliveGreen" GridLines="Both">
                <Columns>
                    <asp:BoundField DataField="BatchNo" HeaderText="BatchNo" HeaderStyle-Width="15%"  />
                    <asp:BoundField DataField="Type" HeaderText="Type" HeaderStyle-Width="15%"  />                                    
                    <asp:ImageField DataImageUrlField="DataLoaded" HeaderText="DataLoaded" HeaderStyle-Width="15%"  />
                    <asp:ImageField DataImageUrlField="Errors" HeaderText="Errors" HeaderStyle-Width="15%"  />
                    <asp:ImageField DataImageUrlField="ProcessingRun" HeaderText="ProcessingRun" HeaderStyle-Width="15%"  />
                </Columns>
            </asp:GridView>

c#

protected void Page_Load(object sender, EventArgs e)
        {
            dataT();
            visualisation.DataBind();

        }

        public void dataT()
        {
            DataTable dtVisu = new DataTable();

            dtVisu.Columns.Add(new DataColumn("BatchNo", typeof(System.String)));
            dtVisu.Columns.Add(new DataColumn("Type", typeof(System.String)));
            dtVisu.Columns.Add(new DataColumn("DataLoaded", typeof(System.String)));
            dtVisu.Columns.Add(new DataColumn("Errors", typeof(System.String)));
            dtVisu.Columns.Add(new DataColumn("ProcessingRun", typeof(System.String)));
            //dtVisu.Columns.Add(new DataColumn("alignRow", typeof(System.String)));


            DataRow dr = dtVisu.NewRow();
            dr["BatchNo"] = "3704500";
            dr["Type"] = "Calibration";
            dr["DataLoaded"] = "images/g4-12.png";
            dr["Errors"] = "images/g4-12.png";
            dr["ProcessingRun"] = "images/g4-12.png";
            //dr["alignRow"] = "1";
            dtVisu.Rows.Add(dr);


            dr = dtVisu.NewRow();
            dr["BatchNo"] = "3704542";
            dr["Type"] = "Range Settings";
            dr["DataLoaded"] = "images/r4-12.png";
            dr["Errors"] = "images/r4-12.png";
            dr["ProcessingRun"] = "images/g4-12.png";
            dtVisu.Rows.Add(dr);

            dr = dtVisu.NewRow();
            dr["BatchNo"] = "3704542";
            dr["Type"] = "Range Settings";
            dr["DataLoaded"] = "images/r4-12.png";
            dr["Errors"] = "images/r4-12.png";
            dr["ProcessingRun"] = "images/g4-12.png";
            dtVisu.Rows.Add(dr);

            dr = dtVisu.NewRow();
            dr["BatchNo"] = "3687345";
            dr["Type"] = "Calibration";
            dr["DataLoaded"] = "images/g4-12.png";
            dr["Errors"] = "images/g4-12.png";
            dr["ProcessingRun"] = "images/g4-12.png";
            dtVisu.Rows.Add(dr);

            dr = dtVisu.NewRow();
            dr["BatchNo"] = "3694217";
            dr["Type"] = "Calibration";
            dr["DataLoaded"] = "images/g4-12.png";
            dr["Errors"] = "images/g4-12.png";
            dr["ProcessingRun"] = "images/g4-12.png";
            dtVisu.Rows.Add(dr);



            visualisation.DataSource = dtVisu;
        }

protected void OnDataBound(object sender, EventArgs e)
        {
            int RowSpan = 2;
            for (int i = visualisation.Rows.Count - 2; i >= 0; i--)
            {
                GridViewRow currRow = visualisation.Rows[i];
                GridViewRow prevRow = visualisation.Rows[i + 1];

                if (currRow.Cells[0].Text == prevRow.Cells[0].Text)
                {
                    currRow.Cells[0].RowSpan = RowSpan;
                    prevRow.Cells[0].Visible = false;
                    RowSpan += 1;

                    currRow.BackColor = Color.FromName("#7AA5D6");
                    prevRow.BackColor = Color.FromName("#7AA5D6");
                }
                else
                {
                    RowSpan = 2;
                }
            }
        }

Upvotes: 0

Views: 1931

Answers (1)

Andrei
Andrei

Reputation: 56688

You need to keep a separate counter of your rows to assign alternating colors. Note that in current code colors alternate only if rows are spanned, therefore single row is not hghlighted. Something around this should do the trick:

protected void OnDataBound(object sender, EventArgs e)
{
    int RowSpan = 2;
    // actual row counter, spanned rows count as one
    int rowCount = 0;
    for (int i = visualisation.Rows.Count - 2; i >= 0; i--)
    {
        GridViewRow currRow = visualisation.Rows[i];
        GridViewRow prevRow = visualisation.Rows[i + 1];

        if (currRow.Cells[0].Text == prevRow.Cells[0].Text)
        {
            currRow.Cells[0].RowSpan = RowSpan;
            prevRow.Cells[0].Visible = false;
            RowSpan += 1;
        }
        else
        {
            RowSpan = 2;
            //it was a new row
            rowCount++;
        }

        if (rowCount % 2 == 0)
        {
            currRow.BackColor = Color.FromName("#7AA5D6");            
        }
    }
}

Upvotes: 3

Related Questions