Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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

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

like image 245
Apollo Avatar asked Dec 26 '22 06:12

Apollo


1 Answers

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.

GridViewRow.DataItem Property

// 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;
   }
}
like image 65
Win Avatar answered Jan 13 '23 11:01

Win