Como fazer para os numeros > que 0 ficarem da cor vermelho?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Display the company name in italics.
e.Row.Cells[6].Text = "<i>" + e.Row.Cells[6].Text + "<i>";
}
}
O Evento está correto!
Coloque assim:
protected void Grid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int value = 0;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (int.TryParse(e.Row.Cells[1].Text, out value))
{
if (value < 0)
{
e.Row.Cells[1].ForeColor = Color.Red;
}
}
}
}
Código Completo:
public partial class GridViewPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Load_Grid();
}
}
public void Load_Grid()
{
Grid1.DataSource = new object[]
{
new {Id = 1, Status = 1},
new {Id = 2, Status = -1},
new {Id = 3, Status = -2},
new {Id = 4, Status = 10}
}
.ToArray();
Grid1.DataBind();
}
protected void Grid1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int value = 0;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (int.TryParse(e.Row.Cells[1].Text, out value))
{
if (value > 0)
{
e.Row.Cells[1].ForeColor = Color.Red;
}
}
}
}
}
ASPX Completo
<%@ Page Language="C#"
AutoEventWireup="true"
CodeBehind="GridView.aspx.cs"
Inherits="WebApplication1.GridViewPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server"
ID="Grid1"
OnRowDataBound="Grid1_RowDataBound"></asp:GridView>
</div>
</form>
</body>
</html>
Não funcionou!
Meus códigos
ASP
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataSourceID="SqlEstoquePecas" ForeColor="Black" GridLines="Horizontal" style="font-size: 12px" Width="100%" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="BANDEIRA" HeaderText="Bandeira" SortExpression="BANDEIRA" />
<asp:BoundField DataField="NOME" HeaderText="Empresa" SortExpression="NOME" >
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="QTDE_ITENS" DataFormatString="{0:N0}" HeaderText="Qtde Itens" SortExpression="QTDE_ITENS" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="REPOSICAO_TOTAL" DataFormatString="{0:c2}" HeaderText="Valor Reposição" SortExpression="REPOSICAO_TOTAL" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="CUSTO_FORNECEDOR_VENDA" DataFormatString="{0:c2}" HeaderText="Custo Reposição Venda" SortExpression="CUSTO_FORNECEDOR_VENDA" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="CUSTO_FORNECEDOR_COMPRA" DataFormatString="{0:c2}" HeaderText="Custo Reposição Compra" SortExpression="CUSTO_FORNECEDOR_COMPRA" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="DIFERECA_VENDA_COMPRA" DataFormatString="{0:c2}" HeaderText="Compra x Venda" SortExpression="DIFERECA_VENDA_COMPRA" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
<asp:SqlDataSource ID="SqlEstoquePecas" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString4 %>" ProviderName="<%$ ConnectionStrings:ConnectionString4.ProviderName %>" SelectCommand="SELECT "BANDEIRA", "NOME", "QTDE_ITENS", "REPOSICAO_TOTAL", "CUSTO_FORNECEDOR_VENDA", "CUSTO_FORNECEDOR_COMPRA", "DIFERECA_VENDA_COMPRA" FROM "I2_ESTOQUE_PECAS_COMPRA_VENDA""></asp:SqlDataSource>
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace globo_intranet.pecas
{
public partial class estoque_pecas : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int value = 0;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (int.TryParse(e.Row.Cells[6].Text, out value))
{
if (value < 0)
{
e.Row.Cells[6].ForeColor = System.Drawing.Color.Red;
}
}
}
}
}
}
Um detalhe, a coluna que desejo aplicar o código é formato em MOEDA.
Quando fazemos uma pergunta, colocamos todas as informações que fazem parte, seria muito fácil te ajudar!
Um debug
arrumaria isso com certeza e troque int
por double
Assim:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
double valor = 0;
if (double.TryParse(e.Row.Cells[1].Text, out valor))
{
if (valor > 0)
{
e.Row.Cells[1].ForeColor = System.Drawing.Color.Red;
}
}
}
}
O código passado é correto e um norte para sua duvida! aprenda a debugar para ver o valor que está chegando …