简体   繁体   English

在GridView的1个单元格中更改文本颜色

[英]Change text color in 1 cell in gridview

I have a gridview with this column: 我在此列有一个gridview:

<asp:TemplateField HeaderText="Importe" SortExpression="importe">
<EditItemTemplate>
    <asp:Label ID="lblImporte" runat="server" Text='<%# Eval("importe") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
    <asp:Label ID="lblImporte" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "importe", "{0:#,##0.00}") %>'></asp:Label>
</ItemTemplate>
<ItemStyle ForeColor="Red" />

By default, the text color is red. 默认情况下,文本颜色为红色。 Comparing programmatically two amounts I need to change the Font color. 以编程方式比较两个量,我需要更改字体颜色。

In my code I have 在我的代码中

Label lblImporte = (Label)gvTablaMes.Rows[e.RowIndex].FindControl("lblImporte");

I tried with this without success. 我尝试了这个但没有成功。

lblImporte.ForeColor = System.Drawing.Color.Green;

I think that I have to use something like that, but I dont know how to use the index for the column (I wrote an X) 我认为我必须使用类似的方法,但是我不知道如何为该列使用索引(我写了一个X)

gvTablaMes.Rows[e.RowIndex].Cells[X].ForeColor = System.Drawing.Color.Green;

You have traversed the gridview in normal mode. 您已在普通模式下遍历了gridview。 But the label control you need to find is inside the edittemplate. 但是您需要找到的标签控件在edittemplate内部。
For that include the following Rowdatabound event in the codebehind file. 为此,在代码隐藏文件中包含以下Rowdatabound事件。

protected void gvTablaMes_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowState == DataControlRowState.Edit) || (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)))
        {
            // the above checking is to verify whether the rows as well as alternating rows are in edit mode
            Label lblImporte = (Label)e.Row.FindControl("lblImporte");
            lblImporte.ForeColor = System.Drawing.Color.Green;
        }
    }

And dont forget to include the OnRowDataBound="gvTablaMes_RowDataBound" to the gridview tag. 并且不要忘记将OnRowDataBound =“ gvTablaMes_RowDataBound”包含到gridview标签中。
Hope this helps.Let me know of any issues in this. 希望能对您有所帮助。让我知道其中的任何问题。
Happy coding :) 快乐的编码:)

try changing in page_load() method for example, 例如,尝试更改page_load()方法,

protected void Page_Load(object sender, EventArgs e)
{

    Label1.ForeColor = System.Drawing.Color.Orange;

}

<form id="form1" runat="server">
<div>

    <asp:Label ID="Label1" runat="server"></asp:Label>

</div>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM