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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15907217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:25:25  来源:igfitidea点击:

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

c#asp.netgridview

提问by Apollo

I have Gridview in ASP.net that displays data. 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.

我在 ASP.net 中有显示数据的 Gridview。根据数据,它会根据单元格的值更改颜色和文本。当列不是模板字段时,这可以正常工作。

 //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.

现在我将列转换为模板字段,但没有任何效果。

     //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

我得到了颜色,但现在我需要将文本更改为以下内容。如果 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.

你们中的任何人都知道如何解决这个问题。提前致谢。

采纳答案by Win

The reason doesn't work in template column is status value is 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;
   }
}

回答by Amit Singh

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;
   }
}

回答by SMHasnain

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.

我真的希望它有效。

回答by Leo Semenov

Use ItemTemplateand surround Evalwith 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>