Code Ratchet
Code Ratchet

Reputation: 6029

How to color the first row in a asp:grid

Good Morning/Afternoon all,

I have a ASP:grid which displays current versions of Terms and Condition that we have currently used/using the first row in the grid is always the one we are currently using and i need to highlight that row but im having trouble trying to highlight it

Heres the asp:gridview

<asp:GridView runat="server" ID="grvTermsAndConditions" AutoGenerateColumns="false"
                    OnRowDataBound="grvTermsAndConditions_rowDataBound" Style="margin-bottom: 20px;">
                    <Columns>
                        <asp:TemplateField HeaderText="CurrentVersion">
                            <ItemTemplate>
                                <asp:Label ID="lblVersion" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CurrentVersion") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Added">
                            <ItemTemplate>
                                <asp:Label ID="lblDateAdded" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Added") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="CreatedBy">
                            <ItemTemplate>
                                <asp:Label ID="lblCreatedBy" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CreatedBy") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

This is my code behind where im trying to get the first row and color is red

protected void grvTermsAndConditions_rowDataBound(Object sender, GridViewRowEventArgs e)
        {

            for (int i = 0; i < grvTermsAndConditions.Rows.Count; ++i )
            {
                if (i == 0)
                {
                    e.Row.CssClass = "gvRowRed";
                    e.Row.Cells[0].CssClass = "white";
                    e.Row.Cells[1].CssClass = "white";

                }
            }
        }

But every time i run this the second row gets coloured?!?!

Any help would be appreciated.

Upvotes: 1

Views: 5066

Answers (4)

AGuyCalledGerald
AGuyCalledGerald

Reputation: 8150

For the sake of performance, I would recommend the gridview DataBound-event as you only want to change one row:

protected void grvTermsAndConditions_DataBound(object sender, EventArgs e)
    {
        GridViewRow firstRow = grvTermsAndConditions.Rows[0];
        firstRow.CssClass = "gvRowRed";
        //etc.
    }

Thus, you don´t have to loop through all rows which can get interesting with large gridviews.

Upvotes: 0

MartinHN
MartinHN

Reputation: 19802

Note that the RowDataBound event is executed for every row - and what you're doing is you loop through all rows, when you bind each row. Very bad for performance.

Try this:

protected void grvTermsAndConditions_rowDataBound(Object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowIndex == 0)
        {
            e.Row.CssClass = "gvRowRed";
            e.Row.Cells[0].CssClass = "white";
            e.Row.Cells[1].CssClass = "white";

        }
}

Upvotes: 4

stay_hungry
stay_hungry

Reputation: 1448

try this

protected void grvTermsAndConditions_rowDataBound(Object sender, GridViewRowEventArgs e)
    {
             if(e.Row.RowType == DataControlRowType.DataRow) // Use this if condition otherwise exception is occurred
             {
                      if(e.RowIndex == 0)
                      {
                             e.Row.CssClass = "gvRowRed";
                e.Row.Cells[0].CssClass = "white";
                e.Row.Cells[1].CssClass = "white";
                      }
             }            
    }

Upvotes: 0

Karl-Johan Sj&#246;gren
Karl-Johan Sj&#246;gren

Reputation: 17622

The Rows-collection is zero-based so the first row would be i == 0, not i == 1. Though it also seems like a bad idea to loop through all of the rows for every row that is added (since you are doing it in OnRowDataBound.

My general recommendation here is to use the :first-child pseudoclass in CSS to color the first row. http://www.w3schools.com/cssref/sel_firstchild.asp

Upvotes: -1

Related Questions