简体   繁体   English

ASP.NET在模板字段中的Gridview单元格中更改文本和颜色

[英]ASP.NET Change Text and Color in Gridview cell in a Template Field

I have Gridview in ASP.net that displays data. 我在ASP.net中有Gridview显示数据。 Depending on the data it changes color and text depending on the value of the cell. 根据数据,它会根据单元格的值更改颜色和文本。 This works fine when a column is NOT a template field. 当列不是模板字段时,这可以正常工作。

 //WORKS WHEN IS NOT A TEMPLATE FIELD
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
        if (e.Row.Cells[2].Text == "1")
        {

           e.Row.Cells[2].Text = "IN";
           e.Row.Cells[2].BackColor = Color.Blue;
           e.Row.Cells[2].ForeColor = Color.White;
        }

  }

Now I converted the Column in to a Template field and nothing works. 现在我将Column转换为Template字段,但没有任何效果。

     //DOEST NOT WORK WHEN IS a TEMPLATE FIELD
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
         if (e.Row.Cells[2].Text == "1")
         {

             e.Row.Cells[2].Text = "IN";
             e.Row.Cells[2].BackColor = Color.Blue;
             e.Row.Cells[2].ForeColor = Color.White;
         }

     }

I GOT THE COLOR WORKING, but now I need to change the Text to the following. 我得到了颜色工作,但现在我需要将文本更改为以下内容。 IF statusID == 1 then display IN, else if statusID == 2 then display OUT IF statusID == 1然后显示IN,否则如果statusID == 2则显示OUT

<asp:TemplateField HeaderText="StatusID" SortExpression="StatusID">
                            <EditItemTemplate>
                                <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue = '<%# Bind("StatusID") %>'>
                                    <asp:ListItem Value="1">IN</asp:ListItem>
                                    <asp:ListItem Value="2">OUT</asp:ListItem>
                                </asp:DropDownList>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblStatus" runat="server" Text='<%# Bind("StatusID") %>' ForeColor='<%# Convert.ToString(Eval("StatusID")) == "1" ? System.Drawing.Color.Green: Convert.ToString(Eval("StatusID")) == "2" ? System.Drawing.Color.Red: System.Drawing.Color.Purple%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>

Any of you know how to solve this issue. 你们任何人都知道如何解决这个问题。 Thanks in advance. 提前致谢。

The reason doesn't work in template column is status value is null. 模板列中的原因不起作用,状态值为null。 Try the following. 请尝试以下方法。

// In template column,
if (e.Row.RowType == DataControlRowType.DataRow)
{
   var status = (Label)e.Row.FindControl("lblStatus");
   if (status.Text == "1")
   {
      e.Row.Cells[2].Text = "IN";
      e.Row.Cells[2].BackColor = Color.Blue;
      e.Row.Cells[2].ForeColor = Color.White;
   }
}

Or cast DataItem to appropiate object and get the status value. 或者将DataItem转换为适当的对象并获取状态值。

GridViewRow.DataItem Property GridViewRow.DataItem属性

// In template column,
if (e.Row.RowType == DataControlRowType.DataRow)
{
   var obj = (MyObject)e.Row.DataItem;
   if (obj.Status == 1)
   {
      e.Row.Cells[2].Text = "IN";
      e.Row.Cells[2].BackColor = Color.Blue;
      e.Row.Cells[2].ForeColor = Color.White;
   }
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
  Label lbl=(Label)e.Row.FindControl("lblStatus");
   if (lbl.Text == "1")
   {
      lbl.Text = "IN";
      e.Row.Cells[2].BackColor = Color.Blue;
      e.Row.Cells[2].ForeColor = Color.White;
   }
}

I am a vb programmar here is a sample code. 我是一个vb programmar这里是一个示例代码。

If e.Row.RowType = DataControlRowType.DataRow Then
                Dim abc As Label = TryCast(e.Row.FindControl("label1"), Label)

            If abc.Text = "ADMIN" Then
                e.Row.Cells(7).ForeColor = Drawing.Color.Blue

            End If
        End If

i really hope it works. 我真的希望它有效。

Use ItemTemplate and surround Eval with any HTML you need, for example, to change your color. 使用ItemTemplate并将Eval包含在您需要的任何HTML中,例如,更改颜色。

Text='<%# "Your HTML(use FONT COLOR=BLUE)" + Eval("HealthUnit") + "Close your HTML here" %>

as, here: 在这里:

<asp:TemplateField HeaderText="Health Unit Website">
    <ItemTemplate>
        <asp:HyperLink runat="server" 
                       NavigateUrl='<%# Eval("Website") %>' 
                       tabindex="-1" 
                       Target="_blank" 
                       Text='<%# "<font color=blue><b>" +
                                  Eval("HealthUnit") + 
                                  "</b></font>" %>'>
        </asp:HyperLink>
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" width="14%"  />
</asp:TemplateField>

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

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