Place a red Gridview line if a cell has the false value

Asked

Viewed 5,178 times

2

I would like a certain line from my GridView turn red if the corresponding cell in the closed column is false.

My Asp.Net is like this:

div class="GridMain">
                    <asp:GridView ID="gvInformationPeriod" runat="server" Width="100%" CssClass="GridViewUser" AllowPaging="True" CellPadding="4" AutoGenerateColumns="False"
                        GridLines="None" ForeColor="#333333" OnPageIndexChanging="gvInformationPeriod_PageIndexChanging" onrowdatabound ="gvInformationPeriod_RowDataBound" PageSize="100">
                        <AlternatingRowStyle CssClass="GridAlternativeUser" BackColor="White" />
                        <Columns>
                            <asp:BoundField DataField="PeriodID" HeaderText="PeriodID" />
                            <asp:BoundField DataField="SchoolID" HeaderText="SchoolID" />
                            <asp:BoundField DataField="DateOpen" HeaderText="Aberto Em" />
                            <asp:BoundField DataField="UserName" HeaderText="Usuario" />
                            <asp:BoundField DataField="PCName" HeaderText="PC Name" />
                            <asp:BoundField DataField="Closed" HeaderText="Closed" />
                        </Columns>
                        <EditRowStyle BackColor="#2461BF" />
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#62AFC1" CssClass="GridPagerUser" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="#EFF3FB" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <SortedAscendingCellStyle BackColor="#F5F7FB" />
                        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                        <SortedDescendingCellStyle BackColor="#E9EBEF" />
                        <SortedDescendingHeaderStyle BackColor="#4870BE" />
                    </asp:GridView>
                </div>

My ASPX.CS carries the GridView thus:

 private void ListarGridInformationPeriod()
    {
        if (ddlSchool.SelectedValue != "")
        {
            gvInformationPeriod.Visible = true;
            gvInformationPeriod.DataSource = Period.ListOpenPeriod(int.Parse(ddlSchool.SelectedValue));
            gvInformationPeriod.DataBind();
        }
    }

protected void gvInformationPeriod_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (!string.IsNullOrEmpty(ddlSchool.SelectedValue))
        {
            gvInformationPeriod.PageIndex = e.NewPageIndex;
            gvInformationPeriod.DataSource = Period.ListOpenPeriod(int.Parse(ddlSchool.SelectedValue));
            gvInformationPeriod.DataBind();
        }
    }

I thought I’d put something conforming down using the RowDataBound but it didn’t work

protected void gvInformationPeriod_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {                
            if (e.Row.Equals("false"))
            {
                e.Row.BackColor = Color.Red;
            }
        }
    }

I tried to use the content of this post but did not understand well

https://stackoverflow.com/questions/5048762/change-gridview-row-color-based-on-condition-in-c-sharp

2 answers

4


Solution:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           if (e.Row.Cells[1].Text.Equals("false"))
           {
                e.Row.BackColor = Color.FromName("red");
           }
      }
}

Where the 1 would be the second column of your Gridview.

2

You almost hit missed telling which field you want to specify the value. Ex: When the value of coluna for false turns red the line. both in protected void GridView1_RowDataBound

Mode 1:

Label lbl1= (Label)e.Row.FindControl("Closed");
if(lbl1.Text = "false")
{
e.Row.BackColor = System.Drawing.Color.Red;
}

Mode 2:

DataRow dr = ((DataRowView)e.Row.DataItem).Row;
  if (dr[1].ToString() == false) //o número é o ID da coluna [0,1,2, etc
{
//seu código
}
  • I want to 'paint red' the whole row that contains the value in the Closed column as false. Thanks.

  • 1

    It sends bullet that will work perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.