I have a 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 the 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 have converted the column to the 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 have color work, but now I need to change the text to the next. IF statusID == 1, then display IN, otherwise, 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 problem. Thanks in advance.
source share